正在与子指令作用域共享作用域对象
Sharing scope object with child directive scopes
我有一个父指令,它在link
期间创建第三方对象的实例,并且该变量需要可由子指令访问。
然而,有两个限制:
- 每个页面可能有多个这样的实例,所以javascript文件顶部的singleton不起作用
- 子指令是递归的,因此它们必须创建自己的作用域
我能想到的唯一方法是将该值作为属性传递给每个子指令。这感觉效率很低,但考虑到上述限制,我看不出有其他选择。
// Some imaginary third-party object
function Tree() {}
// Root directive which creates an instance of the object, links to the page, and loads the data needed.
app.directive('tree', function() {
return {
restrict: 'E',
replace: true,
template: '<div><nodes nodes="nodes"></nodes></div>',
scope: {
nodes: '='
},
link: function(scope, $element) {
// This value needs to be accessible to all child directives
scope.tree = new Tree();
}
};
});
// A directive to render an array of nodes
app.directive('nodes', function() {
return {
restrict: 'E',
replace: true,
template: '<ol>' +
'<li ng-repeat="node in nodes track by node.id">' +
'<node node="node"></node>' +
'</li>' +
'</ol>',
scope: {
nodes: '='
}
};
});
// A directive to render a single node, and recursively any child nodes
app.directive('node', ['$compile', function($compile) {
return {
restrict: 'E',
replace: true,
scope: {
node: '='
},
template: '<div><span ng-bind="node.text"></span></div>',
link: function(scope, $element) {
if (scope.node.children && scope.node.children.length > 0) {
console.log(scope.node.children);
var tmpl = '<nodes nodes="node.children"></nodes>';
var children = $compile(tmpl)(scope);
$element.append(children);
}
// @todo Here's a good example of where I need access to scope.tree
}
};
}]);
我能想到的唯一解决方案是将tree: '='
添加到scope
对象,然后将tree="tree"
传递给每个子对象。
Plunker
一个解决方案是不隔离作用域,这样子指令将从其父作用域继承。如果父级具有scope.tree = new Tree()
,则嵌套指令将继承scope.tree
。
但是,由于您提到每个页面可以有多个这样的实例,这意味着您可能需要一个独立的范围(这就是您目前拥有的范围)。将数据传递到隔离作用域的唯一方法是通过标记中的指令属性。
我想你已经回答了自己的问题:)
相关文章:
- 在Angular Bootstrap Modal和父控制器之间共享作用域
- AngularJS:如何与其他控制器共享作用域函数和变量
- AngularJS:与同级或后代作用域共享作用域行为
- 在控制器之间共享作用域变量的最佳实践是什么
- 在Node中与jsdom和jquery共享变量作用域
- 在指令作用域和控制器$scope之间共享数据
- 多个指令需要共享同一个私有作用域
- 正在与子指令作用域共享作用域对象
- AngularJS-在控制器之间共享作用域值
- 如何在不同的JS文件之间共享相同的作用域变量
- Angular中两个不同的指令在同一个控制器上共享作用域
- 如何在angularjs中使用两个不同的ui路由器在状态之间共享作用域数据?
- AngularJS:在两个具有隔离作用域的指令之间共享一个控制器作用域
- 角指令中link和controller作用域共享的混淆
- Angularjs:拥有相同对象数据的2个不同作用域共享相同行为
- 多个指令共享同一个作用域
- 可以共享作用域和访问实例的私有原型方法
- Angularjs:两个视图,同一个控制器——共享作用域
- 变量在HTTP $作用域中不可共享
- AngularJS中的父控制器'作用域vs共享服务