在AngularJS中解析resolve之前加载控制器

Ada*_*dam 5 javascript angularjs

在加载控制器之前,我的AngularJS解析没有完成.

背景:假设我有一个请求的"我的个人资料"页面(用户加载website.com/user).我想有角度拦截请求并从服务器请求适当的数据.

服务器将以不同的方式回复,具体取决于用户是否登录/ active/etc. 我想在加载模板之前以及在启动适当的控制器之前从服务器获取数据.到目前为止它没有.

App.js代码:

.when('/user',
  {
    templateUrl:'/templates/user',
    controller: 'my_profile',
    resolve: {
        some_cute_function_name: function($timeout){
            $timeout(function(){
                console.log('here123');
            },5)
        }
   }
Run Code Online (Sandbox Code Playgroud)

同时,控制器代码:

            console.log('about to update scope');
Run Code Online (Sandbox Code Playgroud)

事实证明,控制器在解析完成之前启动.我知道这是因为$ timeout之前的秒数越大,我就越有可能看到这个:

about to update scope
here123
Run Code Online (Sandbox Code Playgroud)

而不是相反.

有没有办法确保在启动控制器之前解决问题?

非常感激!

Ben*_*esh 5

你的解析器需要返回一个承诺:

  .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);
        }
   }
Run Code Online (Sandbox Code Playgroud)