angularjs,等待授权结果再转到下一个控制器
您好,我有一个简单的登录服务,它看起来像:angularjs,等待授权结果再转到下一个控制器,angularjs,Angularjs,您好,我有一个简单的登录服务,它看起来像: this.notifyAuthorization = function() { console.log('notifyAuthorization()'); $http({ url: 'app/?cmd=authorization/', }).then(function (response) { console.log('LoginStatusResult: '); console.log(response.d
this.notifyAuthorization = function() {
console.log('notifyAuthorization()');
$http({
url: 'app/?cmd=authorization/',
}).then(function (response) { console.log('LoginStatusResult: '); console.log(response.data);
var data = response.data;
if(data.hash !== undefined) {
$rootScope.loginStatus= true;
hash = data.hash;
}
});
};
这是服务电话
app.run(function($rootScope, $location, LoginService) {
LoginService.notifyAuthorization();
console.log('notifyAuthorization Finished');
});
在看到LoginStatusResult
之前,我看到notifyAuthorization Finished
其他控制器已经初始化,应该知道LoginStatus,那么如何解决这个问题呢?还是有更好的做法?
感谢使用,默认情况下,$http函数会提供:
this.notifyAuthorization = function() {
return http({ //notice the return inserted here
url: 'app/?cmd=authorization/',
}).then(function (response) { console.log('LoginStatusResult: '); console.log(response.data);
var data = response.data;
if(data.hash !== undefined) {
$rootScope.loginState = true;
hash = data.hash;
}
});
};
并在应用程序中正确使用。运行:
app.run(function($rootScope, $location, LoginService) {
LoginService.notifyAuthorization().then(function(){
$rootScope.authorized = true;; //executed after your $http request...
});
});
在html代码中:
<div ng-controller="myController" ng-if="$root.authorized">
<!-- your inner code..
<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam,
feugiat vitae, ultricies eget, tempor sit amet, ante. </p>
-->
</div>
这样,您的控制器将仅在.then函数解析了默认情况下由$http函数提供的用法后实例化:
this.notifyAuthorization = function() {
return http({ //notice the return inserted here
url: 'app/?cmd=authorization/',
}).then(function (response) { console.log('LoginStatusResult: '); console.log(response.data);
var data = response.data;
if(data.hash !== undefined) {
$rootScope.loginState = true;
hash = data.hash;
}
});
};
并在应用程序中正确使用。运行:
app.run(function($rootScope, $location, LoginService) {
LoginService.notifyAuthorization().then(function(){
$rootScope.authorized = true;; //executed after your $http request...
});
});
在html代码中:
<div ng-controller="myController" ng-if="$root.authorized">
<!-- your inner code..
<p>Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam,
feugiat vitae, ultricies eget, tempor sit amet, ante. </p>
-->
</div>
这样,您的控制器将仅在.then函数解析后实例化这是因为
notifyAuthorization()
是一个异步函数,这意味着它在完成之前不会阻止其余代码。你可以用承诺来解决这个问题。检查。这是因为notifyAuthorization()
是一个异步函数,这意味着它在完成之前不会阻止其余代码。你可以用承诺来解决这个问题。查看。这对我来说也差不多,因为我需要在控制器初始化之前知道LoginStatus,然后在notifyAuthorization then函数中添加一个ng if变量。有关详细信息,请参阅我的更新答案。假设我有一个LoginController,如果root.authorized
为TRUE,则无需调用它,默认情况下root.authorized
为FALSE,因此,它将调用LoginController,然后在收到结果时,NG-IF将隐藏控制器。在这种情况下,我可以做什么?这对我来说是一样的,因为我需要在控制器初始化之前知道LoginController的状态,然后在notifyAuthorization then函数中添加一个NG IF变量。有关详细信息,请参阅我的更新答案。假设我有一个LoginController,如果root,则无需调用它。默认情况下,authorized
为TRUE,默认情况下root。authorized
为FALSE,因此它将调用LoginController,然后在收到结果时调用NG-if,它将隐藏控制器。在这种情况下,我能做什么?