【发布时间】:2013-10-04 01:53:34
【问题描述】:
我现在有一些角度方面的经验,但我仍然无法弄清楚那个。我有一个指令 2 通过另一个指令 1 嵌入。 Directive1 与控制器位于同一元素上,并且它们共享相同的范围。我希望directive2 从控制器的范围继承,但不是父/子,而是兄弟姐妹。我在这里错过了什么?
代码如下:
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/
【问题讨论】:
-
我认为您需要设置指令之间的分层“要求”链接。查看有关分层指令egghead.io/lessons/angularjs-directive-communication 的本教程
标签: angularjs angularjs-directive angularjs-scope