bootstraping角度值提供者

时间:2015-08-26 23:24:23

标签: angularjs

我的app.js,service.js,controller.js声明如下。我的问题是控制器只拾取service.js中设置的初始值{userId:-1,networkName:'xyz'},即使在myApp中将值更改为{userId:129,networkName:'mydomainaccoutname'}。 app.js中的run()块。我已正确地将值提供程序注入myApp.run()以及控制器。如何让控制器获取更新的值?感谢。

app.js

(function () {
    'use strict';

    //debugger;
    var myApp = angular.module('myApp', [
        // Angular modules 
        'ngAnimate',        // animations
        //'ngRoute',          // routing
        'ngSanitize',       // sanitizes html bindings (ex: sidebar.js)
        'ui.router',        // state routing
        'ui.grid',
        'ui.grid.pagination',
        'ngResource',       // RESTful resource


        // Custom modules 
        'common',           // common functions, logger, spinner
        'common.bootstrap', // bootstrap dialog wrapper functions

        // 3rd Party Modules
        'ui.bootstrap'      // ui-bootstrap (ex: carousel, pagination, dialog)
    ]);

    myApp.run(['$templateCache', '$rootScope', '$state', '$stateParams', 'currentUserAccount', 'userFactory',
    function ($templateCache, $rootScope, $state, $stateParams, currentUserAccount, userFactory) {
        //currentUserAccount is a value provider service
        currentUserAccount = { userId: 129, networkName: 'mydomainaccoutname' };
    }]);
})();      

service.js

'use strict';

angular.module('myApp')

  .value('version', '5.0')

  .value('currentUserAccount', {
      userId: -1,
      networkName: 'xyz'
  });

controller.js

(function () { 
    'use strict';
    
    //debugger;
    var controllerId = 'shellCtrl';
    angular.module('cmtApp').controller(controllerId,
        ['$rootScope', 'common', 'config', 'currentUserAccount', shell]);

    function shell($rootScope, common, config, currentUserAccount) {
        var vm = this;
        var logSuccess = common.logger.getLogFn(controllerId, 'success');
        var events = config.events;
        vm.busyMessage = 'Please wait ...';
        vm.isBusy = true;
        vm.isAdmin = false;
        vm.currentUser = currentUserAccount;
        vm.spinnerOptions = {
            radius: 40,
            lines: 7,
            length: 0,
            width: 30,
            speed: 1.7,
            corners: 1.0,
            trail: 100,
            color: '#F58A00'
        };
      
        activate();

        function activate() {
            logSuccess('CMT loaded!', null, true);
            common.activateController([], controllerId);
        }
      };
})();

1 个答案:

答案 0 :(得分:0)

为什么不使用实际服务呢?您可以使用以下服务。

angular.module('app')
  .service('UserService', [function() {
     var currentUser = {
        userId: -1,
        networkName: 'xyz'
     };

     var getCurrentUser = function() {
        return currentUser;
     };

     var setCurrentUser = function(user) {
        currentUser = user;
     }; 

     // public functions
     return {
        getCurrentUser: getCurrentUser,
        setCurrentUser: setCurrentUser
     };

  }]);

然后在您的控制器中,您可以执行以下操作:

(function () { 
    'use strict';

    //debugger;
    var controllerId = 'shellCtrl';
    angular.module('cmtApp').controller(controllerId,
        ['$rootScope', 'common', 'config', 'UserService', shell]);

    function shell($rootScope, common, config, UserService) {
        var vm = this;
        ....
        vm.currentUser = UserService.getCurrentUser();
       ...
      };
})();

然后在您的app runner中

myApp.run(['$templateCache', '$rootScope', '$state', '$stateParams', 'UserService', 'userFactory',
function ($templateCache, $rootScope, $state, $stateParams, currentUserAccount, userFactory) {
    //currentUserAccount is a value provider service
    UserService.setCurrentUser({ userId: 129, networkName: 'mydomainaccoutname' });
}]);