は、私がトップ、親とサブを命じ、ネストされたディレクティブを作成したいです。angularjsネストされたディレクティブのスコープ分離隠蔽親ディレクティブオブジェクト
<div ng-app="app">
<top>
<parent>
<sub global-name="global"></sub>
</parent>
</top>
</div>
そして、私のjsvascriptは次のとおりです。
angular.module("app",[]);
angular.module("app").directive("top",function(){
return {
restrict: "E",
transclude: true,
template: "<div ng-transclude></div>"
}
});
angular.module("app").directive("parent", function(){
return {
restrict: "E",
controller: function($scope){
$scope.global = {
name: "parent directive"
};
},
link: function(scope){
},
transclude: true,
template: "<div ng-transclude></div>"
}
});
angular.module("app").directive("sub", function(){
return {
restrict: "E",
require:"^parent",
scope: {
global: "=globalName"
},
controller: function(){
},
link: function(scope){
scope.title = scope.global;
console.log(scope.global);
},
template: "{{global.name}}"
}
});
これが働いています。 JSfiddle codeはこちらです。しかし;
私は親ディレクティブのスコープを分離する場合、私はサブディレクティブから親のglobal
オブジェクトにアクセスすることはできません。
angular.module("app").directive("parent", function(){
return {
restrict: "E",
controller: function($scope){
$scope.global = {
name: "parent directive"
};
},
link: function(scope){
},
transclude: true,
template: "<div ng-transclude></div>",
scope: {}
}
});
これは機能しません。 Jsfiddle is here。あなたの親とサブディレクティブの両方が分離株のスコープを持っているので、あなたがすることはできませんもちろん
私はパレを渡す場合サブオブジェクトにntオブジェクト、私は2つの方法があります。私は親の分離範囲を設定しません。親のスコープを分離する場合は、親ディレクタにアクセスするためにサブディレクティブのrequireを使用する必要があります。 – barteloma
これは、私はあなたがNG-モデルで、どのような角度・メッセージの作品ということ、それをどのように行うことを、代わりにスコープの親コントローラへのアクセスを、私が行っているまさにです。ディレクティブ間のスコープを使用することは、IMOという本当に悪い考えです。 – Walfrat