延迟加载控制器,直到加载模型而不使用路径

时间:2014-06-13 06:41:05

标签: angularjs

有一种方法可以延迟更改路径,直到使用resolve对象加载模型。

详情请见此处:

Delay changing routes until model loaded

是否可以为控制器执行类似的操作(不使用路由)?我想避免与呈现异步检索的模型相关联的闪烁。

类似的东西:

var resolve = { 
                  data1: function($http) { 
                      return  $http({ url:'/someurl1', method: 'GET'});
                  },
                  data2: function($http) {
                      return $http({url: '/someurl2', method: 'GET'});
                  }
               };

 // I know this doesn't exist, but you get the idea about what I'm trying to do.
 app.resolvers(resolve);

 // Then inject data1 and data2 into the controller.  
 // The controller should not load until data1 and data 2 have been resolved.
 app.controller('myController', function($scope, data1, data2) { ... });

1 个答案:

答案 0 :(得分:-1)

如果您使用的是angular-ui-router,那么您就可以内置该功能。 例如:

myApp.config(function($stateProvider)
{
    $stateProvider
         .state('myState', {
               url: '/',
               controller: 'MyController',
               resolve: {
                    myData: function(MyService)
                    {
                         return MyService.loadAsyncData();
                    }
               }
          }
    }

您可以在此处详细了解: https://github.com/angular-ui/ui-router/wiki