Hai sempre una condizione di gara. Ho un paio di alternative che puoi fare:
1) Utilizzare un servizio. Non sono davvero un fan di questa opzione perché porta al codice Spaghetti. E la maggior parte delle volte non vuoi che il controller funzioni finché non hai effettuato l'accesso. Io preferisco l'opzione 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) Utilizzare un route.resolve. Le risoluzioni sono definite sulla rotta e il controllore caricherà solo una volta che la promessa è stata impostata per essere risolta. Ti ho mostrato un esempio per ui-router
e ng-route
devi scegliere il tuo veleno. Se non usi ui-router
dovresti prenderlo in considerazione.
/* 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;
};
});
Grazie mille, opzione # 2 è risolto – MChan