你需要在這裏提供你的指令。很可能你正在使用一個隔離作用域來打破作用域的父子鏈。我的猜測是,你有這樣的事情:
angular.module('module').directive('childDir', [function() {
return {
scope: {
// Having scope defined as an object makes it an 'isolate' scope
// and breaks the chain between this scope and the parent scope.
}
};
}];
爲了解決這個問題,你可以訪問父控制器直接像這樣:
angular.module('module').directive('childDir', [function() {
return {
require: '^parentCtrl',
link: function ($scope, $element, $attrs, parentCtrl) {
$scope.someFunction = parentCtrl.someFunction; // of course this only works if you make someFunction a public function on the parentCtrl
},
scope: {
// Having scope defined as an object makes it an 'isolate' scope
// and breaks the chain between this scope and the parent scope.
}
};
}];
或者,您可以通過不使你的範圍不分離在你的指令定義中返回一個'範圍'鍵或者將它設置爲{scope:true}(這會給你一個新的子範圍)。另一種選擇是通過直接訪問父範圍(而不是依賴原型繼承)來打破孤立障礙,如下所示:$ scope。$ parent.someFunction()。