angular.js - ocLazyLoad配合ui-router延迟加载controller,controller提示 not a
某草草
某草草 2017-05-15 17:10:08
0
2
535

问题如题。延迟加载进controller文件后,controller提示不存在。

贴代码:

state:

//state
$stateProvider.state('user.performance', {
                url: '/performances',
                views: {
                    '': {
                        controller: 'PerformanceCtrl',
                        templateUrl: 'tpl/user/performance'
                    }
                },
                resolve:{
                  loadPerformanceCtrl: ["$ocLazyLoad", function ($ocLazyLoad) {
                      return $ocLazyLoad.load('/app/performance/performance.js');//js文件有成功加载进来
                  }]
                }
            })

performance.js

;(function () {
    'use strict';
     myApp.controller('PerformanceCtrl', [
            '$scope',
            'PerformanceService',
            function ($scope, PerformanceService) {
                console.log('PerformanceCtrl');
                //...
        ])
   })();
// myApp 在此前加载的js中已经定义过。var myApp = angular.module('zwb', []);

报错: Argument 'PerformanceCtrl' is not a

某草草
某草草

reply all(2)
習慣沉默

I fiddled with it and found:

  • When I create a new module, $ocLazyLoad.load()The loaded component is registered successfully, as shown below

angular.module('testtest', []).controller('PerformanceCtrl', [
    //...
])
  • From the above, I thought of another point in angular. When the module takes no parameters, it is a getter, so try the following:

angular.module('order').controller('PerformanceCtrl', [
    //...
])

ocLazyLoad debug promptocLazyLoad.moduleReloaded order, in this way, the component under performance is loaded and the register is successful.

I have been struggling for a long time, and my understanding of angular and the familiarity with introducing components are still not enough. Thanks for G_Koala_C’s answer, thank you~

PHPzhong

The resolve of routing usually preloads the data used by the current state, and then loads the page after the data is loaded. In your situation, there is no need for lazy loading. Isn't it completely superfluous? I don’t know where your error lies. There are probably two types:
1. Remove lazy loading, which is of no use.
2. Change the controller in views to loadPerformanceCtrl, and resolve has been renamed directly. Not sure if it works.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template