如何在AngularJS中使用自己的作用域*從自定義指令*中訪問父作用域?我正在尋找在指令中訪問“父”范圍的任何方式。任何范圍的組合,轉(zhuǎn)換,要求,從上面?zhèn)魅胱兞?或作用域本身)等等。我完全愿意向后彎曲,但我想避免一些完全不牢靠或無法維護(hù)的東西。例如,我知道我現(xiàn)在可以通過$scope從預(yù)鏈接參數(shù)中迭代它的$sibling用于查找概念性“父”的范圍。我真正想要的是$watch父作用域中的表達(dá)式。如果我能做到的話,我就能完成我在這里想要做的事情:AngularJS-如何用變量呈現(xiàn)部分?重要音符該指令必須在同一父范圍內(nèi)可重用。因此,默認(rèn)行為(作用域:false)對(duì)我不起作用。我需要指令的每個(gè)實(shí)例都有一個(gè)單獨(dú)的作用域,然后我需要$watch駐留在父作用域中的變量。代碼示例值1000個(gè)單詞,因此: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?
// Can I get the $parent from the current $scope?
// Can I pass the $parent scope in as an attribute and define
// it as part of this directive's scope definition?
// What don't I understand about how directives work and
// how their scope is related to their parent?
},
post: function($s, $e, $a, parentControl) {
// Has my situation improved by the time the postLink is called?
}
}
}
};});
如何在AngularJS中使用自己的作用域*從自定義指令*中訪問父作用域?
慕桂英3389331
2019-07-09 16:20:16