如何在angular指令中使用已注册的控制器?

时间:2021-07-15 17:27:40

I have a controller registered like this:

我有一个像这样注册的控制器:

myModule.controller('MyController', function ($scope, ...some dependencies...)
{
    ....

Using ng-controller="MyController" in the HTML it all works fine, but now I want to use this controller as my directive's controller. Some thing like this:

在HTML中使用ng-controller =“MyController”它一切正常,但现在我想使用这个控制器作为我的指令控制器。有点像这样:

otherModule.directive('myDirective', function() {
    return {
        restrict: 'A',
        replace: true,
        controller: ??????????,
        scope: {
            foo: '=',
            blah: '=',
        },
        template: '....'
    }
});

I tired just putting MyController but it errors out saying "MyController is not defined". I'm sure if I just put MyController in the global namespace, it would work fine, but I don't want anything in the global namespace. If it makes a difference, myModule is defined as a dependency for otherModule. How can I get a reference to this controller for my directive to use?

我只是把MyController放了,但是错误地说“MyController没有被定义”。我敢肯定,如果我只是将MyController放在全局命名空间中,它会正常工作,但我不想要全局命名空间中的任何内容。如果它有所不同,myModule被定义为otherModule的依赖项。我怎样才能获得对该控制器的引用以供我的指令使用?

As suggested, I tried $controller('MyController'), but now I am getting the following error:

按照建议,我尝试了$ controller('MyController'),但现在我收到以下错误:

Error: Unknown provider: $scopeProvider <- $scope <- myDirectiveDirective
at Error (<anonymous>)
at http://localhost/resources/angular.js?_=1360613988651:2627:15
at Object.getService [as get] (http://localhost/resources/angular.js?_=1360613988651:2755:39)
at http://localhost/resources/angular.js?_=1360613988651:2632:45
at getService (http://localhost/resources/angular.js?_=1360613988651:2755:39)
at invoke (http://localhost/resources/angular.js?_=1360613988651:2773:13)
at Object.instantiate (http://localhost/resources/angular.js?_=1360613988651:2805:23)
at http://localhost/resources/angular.js?_=1360613988651:4621:24
at otherModule.directive.restrict (http://localhost/resources/app.js?_=1360613988824:862:15)
at Object.invoke (http://localhost/resources/angular.js?_=1360613988651:2786:25) 

I'm not sure what to make of this error. Is there more needed to make this work?

我不知道该怎么做这个错误。还有更多需要使这项工作?

2 个解决方案

#1


25  

It appears that you can just use:

看来你可以使用:

controller: 'MyController' 

IF the controller is in the same module as the directive or a higher level module composed of the module with the directive in it.

如果控制器与指令位于同一模块中,或者由带有指令的模块组成的更高级别模块。

When I tried it with two different modules composed into an app module (one for the controller and one for the directive), that did not work.

当我尝试将两个不同的模块组成一个app模块(一个用于控制器,一个用于指令)时,这不起作用。

#2


10  

The answer you've already accepted works, and in almost all cases should be chosen...

您已经接受的答案有效,几乎在所有情况下都应该选择......

For sake of completeness: Here is how you can use a controller from another module

为了完整起见:以下是如何使用其他模块的控制器

(PS: Don't do this. lol :P)

(PS:不要这样做。哈哈:P)

var app = angular.module('myApp', ['myDirectives']);

app.controller('AppCtrl1', function($scope) {
    $scope.foo = 'bar';
});


var directives = angular.module('myDirectives', []);

directives.directive('test', function($controller) {
   return {
       template: '<h1>{{foo}}</h1>',
       link: function(scope, elem, attrs) {
          var controller = $controller('AppCtrl1', { $scope: scope });

          console.log($scope.foo); //bar
       }
   };
});

#1


25  

It appears that you can just use:

看来你可以使用:

controller: 'MyController' 

IF the controller is in the same module as the directive or a higher level module composed of the module with the directive in it.

如果控制器与指令位于同一模块中,或者由带有指令的模块组成的更高级别模块。

When I tried it with two different modules composed into an app module (one for the controller and one for the directive), that did not work.

当我尝试将两个不同的模块组成一个app模块(一个用于控制器,一个用于指令)时,这不起作用。

#2


10  

The answer you've already accepted works, and in almost all cases should be chosen...

您已经接受的答案有效,几乎在所有情况下都应该选择......

For sake of completeness: Here is how you can use a controller from another module

为了完整起见:以下是如何使用其他模块的控制器

(PS: Don't do this. lol :P)

(PS:不要这样做。哈哈:P)

var app = angular.module('myApp', ['myDirectives']);

app.controller('AppCtrl1', function($scope) {
    $scope.foo = 'bar';
});


var directives = angular.module('myDirectives', []);

directives.directive('test', function($controller) {
   return {
       template: '<h1>{{foo}}</h1>',
       link: function(scope, elem, attrs) {
          var controller = $controller('AppCtrl1', { $scope: scope });

          console.log($scope.foo); //bar
       }
   };
});