angularjs – 如何在我的角度指令中使用已注册的控制器?

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在我的角度指令中使用已注册的控制器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个控制器注册
  1. myModule.controller('MyController',function ($scope,...some dependencies...)
  2. {
  3. ....

在HTML中使用ng-controller =“MyController”它一切正常,但现在我想使用这个控制器作为我的指令的控制器.这样的一些事情

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

我累了只是把MyController,但它错误地说“MyController没有定义”.我确定如果我把MyController放在全局命名空间中,那么这个工作正常,但是我不希望在全局命名空间中使用任何东西.如果它有所作为,myModule被定义为otherModule的依赖关系.我如何获得对这个控制器的引用以供我的指令使用?

如所建议的,我尝试过$controller(‘MyController’),但是现在我收到以下错误

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

我不知道该怎么做这个错误.是否有更多的需要做这项工作?

看来你只能使用:
  1. controller: 'MyController'

如果控制器与指令相同的模块或由其中具有指令的模块组成的更高级别的模块.

当我尝试使用两个不同的模块组成一个应用程序模块(一个用于控制器,一个用于指令),这没有工作.

猜你在找的Angularjs相关文章