加载控制器之前,我的AngularJS解析未完成。
背景:假设我有一个“我的个人资料”页面被请求(用户加载website.com/user)。我想让角度拦截请求并从服务器请求适当的数据。
服务器将根据用户是否已登录/活动/等进行不同的答复。我希望在加载模板之前以及启动适当的控制器之前从服务器获取数据。到目前为止,还没有。
App.js代码:
.when('/user',
{
templateUrl:'/templates/user',
controller: 'my_profile',
resolve: {
some_cute_function_name: function($timeout){
$timeout(function(){
console.log('here123');
},5)
}
}
同时,控制器代码:
console.log('about to update scope');
事实证明,控制器是在解析完成之前启动的。我知道这一点是因为$ timeout结束之前的秒数越长,我越有可能看到以下内容:
about to update scope
here123
而不是相反。
有什么方法可以确保在启动控制器之前解决该问题?
非常感激!
您的解析器需要返回承诺:
.when('/user',
{
templateUrl:'/templates/user',
controller: 'my_profile',
resolve: {
some_cute_function_name: function($timeout){
return $timeout(function(){
console.log('here123');
return { some: 'data' };
},5);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句