我在我的angularJS應用程序中有兩個自定義指令。一個作爲父母,另一個作爲孩子。我正在嘗試訪問子指令內的父級範圍。但我沒有得到所需的輸出。自定義子級指令訪問父級的範圍
<div ng-controller="CountryCtrl">
{{myName}}
<div ng-controller="StateCtrl">
<state nameofthestate="'Tamilnadu'">
<city nameofthecity="'Chennai'"></city>
</state>
</div>
</div>
和我的腳本看起來像
var app = angular.module("sampleApp",[]);
app.controller("CountryCtrl",function($scope){
$scope.myName = "India";
});
app.controller("StateCtrl",function($scope){
});
app.directive("state",function(){return {
restrict : 'E',
transclude: true,
scope : { myName : '=nameofthestate'},
template:"** {{myName}} is inside {{$parent.myName}}<br/><ng-transclude></ng-transclude>"
}});
app.directive("city",function(){return {
restrict : 'E',
require:'^state',
scope : { myName : '=nameofthecity'},
template:"**** {{myName}} is inside {{$parent.myName}} which is in {{$parent.$parent.myName }}<br/> "
}});
相應的jsfiddle提供https://jsbin.com/nozuri/edit?html,js,output
的輸出,我得到是
India
** Tamilnadu is inside India
**** Chennai is inside India which is in Tamilnadu
和預期輸出是
India
** Tamilnadu is inside India
**** Chennai is inside Tamilnadu which is in India
任何人都可以教我在這裏做錯了嗎?
對不起,但你的小提琴不適合我。 –
嗨http://stackoverflow.com/questions/23437113/get-property-value-from-parent-directive-within-child-directive這可能會幫助你它看起來像你想要做的 –
你可以做一個自定義transclude與'transclude:'element''。構造函數在'link:function(scope,element,attrs,Ctlr,transclude){}' – gr3g