你需要在这里提供你的指令。很可能你正在使用一个隔离作用域来打破作用域的父子链。我的猜测是,你有这样的事情:
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()。