我正在寻找任何方式访问指令中的"父"范围.范围,转换,要求的任何组合,从上面传递变量(或范围本身)等.我完全愿意向后弯腰,但我想避免一些完全hacky或不可维护的东西.例如,我知道我现在可以通过$scope从preLink参数中获取并迭代它的$sibling范围来找到概念性的"父".
我真正想要的是能够$watch在父范围内表达.如果我能做到这一点,那么我可以完成我在这里要做的事情:
AngularJS - 如何使用变量渲染部分?
重要的一点是,该指令必须可以在同一父范围内重复使用.因此默认行为(范围:false)对我不起作用.我需要每个指令实例的单独范围,然后我需要$watch一个存在于父范围内的变量.
代码示例值1000个单词,因此:
app.directive('watchingMyParentScope', function() {
return {
require: /* ? */,
scope: /* ? */,
transclude: /* ? */,
controller: /* ? */,
compile: function(el,attr,trans) {
// Can I get the $parent from the transclusion function somehow?
return {
pre: function($s, $e, $a, parentControl) {
// Can I get the $parent from the parent controller?
// By setting this.$scope = $scope from within that controller?
// …Run Code Online (Sandbox Code Playgroud)