小编tgk*_*okk的帖子

如何将控制器用于两个指令?

我有这个代码:

JS:

angular.module("module")
  .controller("fooController", ["$scope", function($scope) {
    ...
  })
  .directive("foo", function() {
    return {
      restrict: "E",
      controller: "fooController",
      link: function($scope, $element, $attrs) {
        // Do some things with the scope of the controller here
      }
    }
  })
  .directive("bar", function() {
    return {
      restrict: "E",
      require: "fooController",
      link: function($scope, $element, $attrs) {
         // Nothing yet
      }
    }
  });
Run Code Online (Sandbox Code Playgroud)

HTML:

<html>
  <head>
    <!-- Scripts here -->
  </head>
  <body ng-app="module">
    <foo/>
    <bar/>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

指令foo有效,但指令bar抛出错误:No controller: fooController. …

angularjs

8
推荐指数
1
解决办法
6566
查看次数

标签 统计

angularjs ×1