我怎么能在我的角度指令使用注册控制器? [英] How can I use a registered controller in my angular directive?

查看:103
本文介绍了我怎么能在我的角度指令使用注册控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册一个这样的控制器:

I have a controller registered like this:

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

使用 NG-控制器=在这一切工作正常的HTMLmyController的,但现在我想用这个控制器作为我的指令的控制器。有些事情是这样的:

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:

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

我厌倦了只是把 myController的,但出现了错误说是myController的未定义。我敢肯定,如果我只是把 myController的在全局命名空间,它的工作很好,但我不希望在全局命名空间什么。如果它的确与众不同, Mymodule中定义为 otherModule 的依赖。我怎样才能得到这个控制器的引用我的指令使用?

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的'),但现在我收到以下错误:

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

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?

推荐答案

看样子,你可以使用:

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.

这篇关于我怎么能在我的角度指令使用注册控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆