我有这个代码:
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
}
}
});
HTML:
<html>
<head>
<!-- Scripts here -->
</head>
<body ng-app="module">
<foo/>
<bar/>
</body>
</html>
指令foo
有效,但指令bar
抛出错误:No controller: fooController
.
如何在保持当前结构的同时解决此问题(控制器不在 HTML 中,但由指令使用,bar
在外部foo
并共享同一个控制器,而两者都在修改其范围)?我在这里阅读了讨论,但我不明白该怎么做。