'Unknown provider' 检索延迟加载的控制器时出错

'Unknown provider' error while retrieving lazy-loaded controller

我希望 'MyController2' 继承 'MyController1',但是,两个控制器都使用 ocLazyLoad 延迟加载。根据Jussi Kosunen对这个问题的回答(),我做了一个函数registerDynamic()来注册控制器,但是仍然报如下错误controller1.js 中带有“$controller”的行:

错误:[$injector:unpr] 未知提供者:$elementProvider <- $element <- DataTableController

我的代码是这样的。

第一个文件controller1.js:

angular.module( 'myApp',[])
    .controller( 'MyController1', [ function($scope){
        // ...
    }]);

第二个文件controller2.js:

angular.module( 'myApp')
    .controller( 'MyController2', [ '$controller', '$scope',function($controller, $scope){
         $controller('MyController1', {$scope: $scope }); // here triggers error '[$injector:unpr] Unknown provider'
         // ...
    }]);

在第三个文件lazy-load.js中,我懒加载了上面两个.js文件:

var app = angular.module('myApp'),
queueLen = app._invokeQueue.length;
app.directive( 'LazyLoad', [ function( ){
    return {
        restrict: 'EA',
        scope: {
           src: '@',
        },
        link: function( scope, element, attr ){
            var registerDynamic = function() {
                // Register the controls/directives/services we just loaded
                var queue = syncreonApp._invokeQueue;
                for(var i=queueLen;i<queue.length;i++) {
                    var call = queue[i];
                    // call is in the form [providerName, providerFunc, providerArguments]
                    var provider = syncreonApp.providers[call[0]];
                    if(provider) {
                        // e.g. $controllerProvider.register("Ctrl", function() { ... })
                        $log.debug("Registering " + call[1] + " " + call[2][0] + " ...");
                        provider[call[1]].apply(provider, call[2]);
                    }
                }
                queueLen = i;
            },
            loadMultipleJs = function ( js_files ){
                var deferred = $q.defer();
                var js_file1 = js_files.shift(),
                    js_file2 = js_files.shift();

                 $ocLazyLoad.load( js_file1 )
                    .then ( function(){
                        registerDynamic();
                        $ocLazyLoad.load( js_file2 )
                            .then ( function(){
                                registerDynamic();
                                deferred.resolve();
                            }, function(){
                                deferred.reject();
                            });
                    }, function(){
                        deferred.reject();
                    });
           };
           jsonOfJsFilesToLoad = JSON.parse(scope.src);
           loadMultipleJs(jsonOfJsFilesToLoad );
       }
   };
}]);

更新

The official Angular documentation for 'Unknown Provider' error 说:

Attempting to inject one controller into another will also throw an Unknown provider error:

也许即使使用 $controller 服务我们也无法将控制器注入花药?

您正在断章取义错误消息。

Attempting to inject one controller into another will also throw an Unknown provider error:

angular.module('myModule', [])
   .controller('MyFirstController', function() { /* ... */ })
   .controller('MySecondController', ['MyFirstController', function(MyFirstController) {
     // This controller throws an unknown provider error because
     // MyFirstController cannot be injected.
   }]);

这不是您实例化控制器的方式。

它具体说:

Use the $controller service if you want to instantiate controllers yourself.

这是您实例化控制器的方式。

在别处寻找您的问题。

您的错误信息:

Unknown provider: $elementProvider <- $element <- DataTableController

我读到这篇文章的方式是,在您的 DataTableController 中,您正试图注入 $element$element 不是服务是 local。使用 $controller 服务将 $element 作为 local 注入:

$controller('DataTableController', {$scope: $scope, $element: value });