Article From:https://segmentfault.com/q/1010000014367710
Question:

The total control file app.js is defined in the root directory
Then, the controller files used in the page are defined under the subfolder of different directory, such as product.js, etc.
The code that is declared in app.js is as follows (excerpt)

var sdWan = angular.module('sdWan', ['ngResource', 'ngRoute', 'oc.lazyLoad'])
    .config(function ($resourceProvider) {
        $resourceProvider.defaults.stripTrailingSlashes = false;
    })
    .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: "pages/home.html",
                controller: "homeCtrl",
                resolve: {
                    deps: ['$ocLazyLoad',
                        function ($ocLazyLoad) {
                            return $ocLazyLoad.load({
                                files: [
                                    'pages/home.js'
                                ]
                            });
                        }
                    ]
                }
            })
            .when('/devicesManage/deviceList', {
                templateUrl: "pages/devicesManage/deviceList/deviceList.html",
                controller: "devCtrl",
                resolve: {
                    deps: ['$ocLazyLoad',
                        function ($ocLazyLoad) {
                            return $ocLazyLoad.load({
                                files: [
                                    'pages/devicesManage/deviceList/deviceList.js'
                                ]
                            });
                        }
                    ]
                }
            })

            .otherwise({
                redirectTo: '/'
            })
    }]);

As the code shows, different pages have different controller, and the ng-controller= name is added to the corresponding page.
And in the JS introduced in the corresponding page, the controller is defined, as shown in the following code (pages/home.js):

sdWan.controller('homeCtrl', function ($scope) {})  // Cannot be definedConsole.log (sdWan) / / normal printing

And these JS are actually loaded into the document. How can the code in controller be unable to execute and report it?
[$controller:ctrlreg] The controller with the name ‘homeCtrl’ is not registered.
Under this file, console.log (sdWan) can be acquired to sdWan. But the definition of controller is a problem.

How should I solve it, please?
Thank you。

Answer 0:

return $ocLazyLoad.load({

name:"Your APP ",Files: ['pages/home.js']

});
// Add the name of your main application module in front of your files.

Leave a Reply

Your email address will not be published. Required fields are marked *