我現在有一些角度的經驗,但我仍然無法弄清楚那一個。我有一個指令2 transcluded通過另一個指令1。指令1和控制器在同一個元素上,它們共享相同的範圍。我希望指令2從控制器的範圍繼承,但不是父母/孩子,他們最終成爲兄弟姐妹。我在這裏錯過了什麼?角度指示範圍問題
下面是代碼:
angular.module('myModule', [])
.controller('Controller', ['$scope', function($scope) {
console.log('from controller', $scope);
}])
.directive('directive1', function(){
return {
restrict: 'A',
replace: true,
transclude: true,
template: '<div style="width:150px;"> <p>directive1</p>'
+'<div style="width:100px;" ng-transclude></div> </div>',
link: function($scope, elem, attrs){
console.log('from directive1', $scope);
}
}
})
.directive('directive2', function(){
return {
restrict: 'A',
link: function($scope, elem, attrs) {
console.log('from directive2', $scope);
}
}
});
相關的HTML:
<div x-directive1 ng-controller="Controller">
<div x-directive2> <p>directive2</p> </div>
</div>
和鏈接到的jsfiddle:http://jsfiddle.net/RFontana/Lm7gA/1/
我認爲你需要設置指令之間的分層「需求」鏈接。看看這個關於分層指令的教程http://egghead.io/lessons/angularjs-directive-communication –