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

时间:2015-10-24 19:26:15

标签: angularjs angular-ui-router angularjs-controller oclazyload

收到此错误......

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

尝试转换到ui-router状态时...

    .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"已注册为控制人。

1 个答案:

答案 0 :(得分:0)

所以,事实证明你不能这样做。以上述方式注册控制器仅适用于引导程序。

但是如果你在config

中存储对$ controllerProvider寄存器函数的引用,你就可以这样做
app.config(function ($controllerProvider) {
    app.lazy = {
        controller: $controllerProvider.register,
    };
}

然后在你的控制器js中使用对寄存器功能的引用添加控制器..

var app = angular.module('application')

function registerController($scope) {

};

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