延迟加载的控制器在转换到状态时导致错误

lazy loaded controller results in error when transitioning to state

收到此错误...

Error: [ng:areq] Argument 'registerController' is not a function, got undefined

尝试转换到 ui 路由器状态时...

    .state('register', {
        url: "/register",
        templateUrl: "App/components/register/registerView.html",
        data: { pageTitle: 'Register', specialClass: 'gray-bg' },
        controller: "registerController",
        resolve: { // Any property in resolve should return a promise and is executed before the view is loaded
            loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
                // you can lazy load files for an existing module
                return $ocLazyLoad.load(['App/components/register/registerController.js']);
            }]
        }
    })

延迟加载的文件被检索到运行,这里供参考。

var app = angular.module('application')

function registerController($scope) {
    $scope.test = "test";
};

app.controller('registerController', registerController);

状态试图解析时抛出异常

controller: "registerController",

属性。但是我知道此时懒加载的代码已经运行并且"registerController"已经注册为控制器了。

所以,事实证明你不能这样做。以上述方式注册控制器仅在 bootstrap 有效。

但是如果你在 config

app.config(function ($controllerProvider) {
    app.lazy = {
        controller: $controllerProvider.register,
    };
}

然后在您的控制器 js 中使用对 register 函数的引用添加控制器..

var app = angular.module('application')

function registerController($scope) {

};

app.lazy.controller('registerController', ['$scope' ,registerController]);