我希望創建一個有序頂部,父和子嵌套指令。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對象來分,我有兩種方法。我不會設置父母的隔離範圍。或者如果我隔離父級的範圍,我應該在子指令中使用require來訪問父級控制器。 – barteloma
這正是我所做的,我訪問父控制器而不是範圍,你如何使用ng-model來實現,以及角度消息是如何工作的。在指令之間使用範圍僅僅是一個非常糟糕的想法IMO。 – Walfrat