angular.js - ocLazyLoad cooperates with ui-router to delay loading the controller, and the controller prompts not a
某草草
某草草 2017-05-15 17:10:08
0
2
558

The question is as mentioned. After lazy loading into the controller file, the controller prompt does not exist.

Post code:

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', []);

Error report: 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