angularjs - AngularJS Transcluded 指令的范围从其他指令捕获
问题描述
我得到了嵌入指令,例如“ aDirective ”和其他随机的“ bDirective ”指令。我的任务是:我想获得一个“ aDirective 的范围变量”并在“ 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
是从顶部开始的指令。然后它可以接收它的控制器作为link
函数参数(第四个)。
.directive('nestedDirective', function(){
return {
require: '^aDirective',
link: function (scope, elements, attrs, aDirectiveController) {
// access aDirectiveController's methods or properties
}
}
})
推荐阅读
- java - 如何修复 EditText 的 getText 方法的覆盖
- r - 如何使用 R 中的 Caret 包按组进行 Box-Cox transorm?
- android - 如何自定义 Android Facebook 登录按钮?
- python-3.x - 为什么这个 queue.join 调用会无限期阻塞?
- excel - 从Excel VBA中的字符串中提取最后一个括号中的字符
- python - 将 JSON 数据加载到 CSV 时,CSV excel 文件中的格式不正确
- javascript - 为什么在超小屏幕尺寸下,单击折叠按钮时无法打开?
- ocaml - Ocaml 打印元组列表
- unity3d - 如何将 uPLibrary 导入 Unity 以使用 HoloLens?
- angular5 - json数据无法在html中访问