AngularJS 嵌入指令的范围从其他指令捕获
AngularJS Transcluded directive's scope catch from other Directive
我得到了嵌入的指令,例如“aDirective”和其他随机的“bDirective”指令。我的任务是:我想得到一个 'aDirective's 范围变量并在 'bDirective'.
中捕获它
angular.module('myApp',[])
.controller('bDirective',['$scope',function(scope){
scope.getScopeVar = function () {
// here I want to get aDirective - s 'someVar' variable
scope.someVar;
debugger;
};
scope.getScopeVar();
}])
.directive('aDirective',function(){
return{
scope:{},
transclude:true,
template:'<div>123</div>',
link: function(scope, element, attrs, controller, transclude){
scope.someVar = 'asd';
transclude(scope, function (clone) {
element.append(clone);
});
}
};
});
有什么解决办法吗?
问候尼克。
嵌套指令应该require
the directive from the top。然后它可以接收其控制器作为 link
函数参数(第 4 个)。
.directive('nestedDirective', function(){
return {
require: '^aDirective',
link: function (scope, elements, attrs, aDirectiveController) {
// access aDirectiveController's methods or properties
}
}
})
我得到了嵌入的指令,例如“aDirective”和其他随机的“bDirective”指令。我的任务是:我想得到一个 'aDirective's 范围变量并在 'bDirective'.
中捕获它angular.module('myApp',[])
.controller('bDirective',['$scope',function(scope){
scope.getScopeVar = function () {
// here I want to get aDirective - s 'someVar' variable
scope.someVar;
debugger;
};
scope.getScopeVar();
}])
.directive('aDirective',function(){
return{
scope:{},
transclude:true,
template:'<div>123</div>',
link: function(scope, element, attrs, controller, transclude){
scope.someVar = 'asd';
transclude(scope, function (clone) {
element.append(clone);
});
}
};
});
有什么解决办法吗? 问候尼克。
嵌套指令应该require
the directive from the top。然后它可以接收其控制器作为 link
函数参数(第 4 个)。
.directive('nestedDirective', function(){
return {
require: '^aDirective',
link: function (scope, elements, attrs, aDirectiveController) {
// access aDirectiveController's methods or properties
}
}
})