Preserve state with Angular UI-Router

后端 未结 3 1471
我寻月下人不归
我寻月下人不归 2021-01-31 10:29

I have an app with a ng-view that sends emails to contact selected from a contact list. When the users select \"Recipient\" it shows another view/page where he can search, filte

3条回答
  •  花落未央
    2021-01-31 11:10

    I'm not sure if this is recommended or not, but I created a StateService to save/load properties from my controllers' scopes. It looks like this:

    (function(){
        'use strict';
    
        angular.module('app').service('StateService', function(){
    
            var _states = {};
    
            var _save = function(name, scope, fields){
                if(!_states[name])
                    _states[name] = {};
                for(var i=0; i

    To use it, I put some code at the end of my controller like this:

    angular.module('app').controller('ExampleCtrl', ['$scope', 'StateService', function ($scope, StateService) {
        $scope.keyword = '';
        $scope.people = [];
    
        ...
    
        var saveStateFields = ['keyword','people'];
        $scope = StateService.load('ExampleCtrl', $scope, saveStateFields);
        $scope.$on('$destroy', function() {
            StateService.save('ExampleCtrl', $scope, saveStateFields);
        });
    }]);
    

提交回复
热议问题