本文介绍了AngularJS $rootScope.$broadcast 在 app.run 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 AngularJS .run 下有以下代码,它在我的本地开发机器上完美运行,但在上传到我的客户端服务器时不起作用......经过几次测试后,很明显,当控制器加载时事件尚未触发,因此取决于此事件的控制器中的大多数功能都不起作用.有人可以告诉我我在这里做错了什么以及如何解决吗?谢谢

I have the following code under my AngularJS .run which works perfectly on my local development machine but won't work when uploaded to my client server...after few tests it is obvious that by the time the controller is loaded the event is not triggered yet so most of the functions in the controller depending on this event do not work. Can someone please tell me what I am doing wrong here and how to fix it? Thanks

myApp.run(['AuthDataSvc', '$rootScope', function (AuthDataSvc, $rootScope) {
    AuthDataSvc.getAuth().then(function(Token){
        $rootScope.$broadcast('Token', Token);
    }, function(status){
        console.log(status);
    });
}]);

推荐答案

您总是会遇到竞争条件.我有几个你可以做的选择:

You are always going to have a race condition. I have a couple alternatives you can do:

1) 使用服务.我不是很喜欢这个选项,因为它会导致意大利面代码.大多数情况下,您不希望控制器在您登录之前运行.我更喜欢选项 2.

1) Use a service. I am not really a fan of this option because it leads to Spaghetti code. And most time you don't want the controller to run until you have logged on. I prefer option 2.

 myApp.run(['AuthDataSvc', '$rootScope', function (AuthDataSvc, $rootScope) {
    AuthDataSvc.getAuth(); /* no op we will use the service to determine logged in */
}]);


/* inside a controller */
if(AuthDataSvc.isLoggedIn()){
      //do something.
}

2) 使用 route.resolve.解析是在路由上定义的,控制器只会在承诺被设置为解析后才加载.我向你展示了一个 ui-routerng-route 的例子,你需要选择你的毒药.如果你不使用 ui-router 你应该考虑它.


2) Use a route.resolve. Resolves are defined on the route and the Controller will only load once it the promise has been set to resolved. I showed you an example for ui-router and ng-route you need to pick your poison. If you dont use ui-router you should consider it.

/* app.config ... route config.. */
var waitForLogon = {
      UserToken: ["AuthDataSvc", function (AuthDataSvc) {
         return AuthDataSvc.logon();
      }]
};

//this is for ng-route
 $routeProvider
   .when('/Book/:bookId', {
      templateUrl: '--',
      controller: 'MyCtrl',
      resolve: waitForLogon
   })


//this is for ui-router
$stateProvider
     .state('me', {
            templateUrl: '--',
            controller: 'MeCtrl',
            resolve: waitForLogon
 })
/* controller */
 angular.module('yourapp')
    .controller('MyCtrl', ["UserToken", ... , function(UserToken){
                  //User Token will always be here when your Ctrl loads.
   });
/* service code -- */
angular.module('yourapp')
    .service('AuthDataSvc', ["LogonModel", "$q", function(LogonModel, $q) {
        this._q = null;
        var that = this;
        this._doAuth = function(){
           this.getAuth().then(function(Token){ that._q.resolve(Token) }, function(error){that._q.reject(error);}
        };
        this.logon = function () {
            if(!this._q){
              this._q = $q.defer();
               this._doAuth();// <-current auth do here, and resolve this._q when done
            }
            return this._q.promise;
        };
    });

这篇关于AngularJS $rootScope.$broadcast 在 app.run 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-25 23:54
查看更多