使用OcLazyLoad在控制器加载之前解析资源调用

时间:2015-09-19 10:54:33

标签: angularjs angular-ui-router oclazyload

我需要使用$ resource提供程序对我的webservice执行http调用。 我想首先解析来自http调用的数据,然后将此结果注入控制器。 根据OcLazyLoad的思考,我刚刚编写了这段代码。 在我看来它应该:

  • 首次加载服务;
  • 拨打http电话;
  • 解决承诺后,加载控制器

    .state('app.user.customer.detail', {
    url: '/{id}',
    templateUrl: "assets/views/customerDetail.html",
    resolve: { loadMyService: ['$ocLazyLoad', '$injector', 
                    function($ocLazyLoad, $injector) {
            return $ocLazyLoad.load('assets/js/services/customer.js').then(
                        function() {                            
                            return $ocLazyLoad.load('assets/js/services/customerFirstService.js').then(function() {
    
                                var $serviceTest = $injector.get("CustomerFirstLoad");
                                $serviceTest.testLoad();
    
                                }).then(function(){
                                            return $ocLazyLoad.load(['assets/js/controllers/customerCtrl.js']);
                                  });
                            });
    
                     }]}
    

这是服务

'use strict';

app.factory('CustomerFirstLoad', ['$q', '$timeout', function Customers($q, $timeout) {
    var svc = {};

    svc.testLoad = function(){
        var deferrer = $q.defer(); 

        $timeout(function(){
            console.log("response");
            deferrer.resolve("response");
        }, 3000);
      return deferrer.promise;
  };

return svc;
}]);

一些建议?

1 个答案:

答案 0 :(得分:4)

试试这个

$scope.gridOptions.columnDefs.forEach(function (c) {
var term = c.filter.term
}

您需要以这种方式返回.state('app.user.customer.detail', { url: '/{id}', templateUrl: "assets/views/customerDetail.html", resolve: { loadMyService: ['$ocLazyLoad', '$injector', function($ocLazyLoad, $injector) { return $ocLazyLoad.load('assets/js/services/customer.js').then( function() { return $ocLazyLoad.load('assets/js/services/customerFirstService.js').then(function() { var $serviceTest = $injector.get("CustomerFirstLoad"); return $serviceTest.testLoad(); // <-- CHANGED HERE }).then(function(){ return $ocLazyLoad.load(['assets/js/controllers/customerCtrl.js']); }); }); }]} 的承诺$serviceTest.testLoad()在[{1}}完成之前不会继续。