我試圖達到父「框」指令遞歸的控制器:如何通過遞歸地訪問父指令的控制器?
<body ng-app="main">
<!-- no nesting: parent is the just body -->
<box></box>
<script type="text/javascript">
angular.module('main', [])
.directive('box', function() {
return {
restrict: 'E',
controller: function() { },
require: '?^box', // find optional PARENT "box" directive
link: function(scope, iElement, iAttrs, controller) {
// controller should be undefined, as there is no parent box
alert('Controller found: ' + (controller !== undefined));
}
};
});
</script>
</body>
我期望控制變量是在鏈接功能undefined
,但我得到的實際箱指令的控制器。
所以我的問題是...如何獲得這樣的訪問父控制器的情況下:
<box>
<box></box>
</box>
取代
require: '?^box',
你可以看到,有我原來的例子沒有父框,所以我希望得到'undefined'作爲第四個參數的鏈接功能,而不是實例本身的控制器 – maX但是你聲明瞭一個控制器。所以父母得到那個控制器和孩子(如果有的話)繼承它。你的目標是要知道一個特定的指令是父母還是孩子?如果是這樣,也許你想看看$父範圍? – KayakDave
其實我不想在這種情況下使用範圍,因爲範圍可能會變得棘手,特別是如果您需要使用隔離範圍。 另外,最好的做法是使用控制器和'require'進行指令到指令的通信。 我的觀點是,當你需要一個不同名字的父指令時,它就像一個魅力一樣,你爲什麼不能夠遞歸地做到這一點。 – maX