为控制器之间的数据交换创建一项服务
嵌套树选项
在服务中创建一个具有父->子结构的对象,在嵌套模板中存储一些唯一的 id 来告诉服务在嵌套树中存储数据的位置
在模板上下文中,它看起来很干净,但所有逻辑都将转移到服务中
所以在模板中你会有类似的东西:
<input type="checkbox" ng-checked="checked">
在控制器中:
$scope.uniqueid='3je99ehr0jer';//generate something...
$scope.checked=false;
yourService.set($scope.uniqueid,$scope.$parent.uniqueid,$scope.checked,function(checked){
$scope.checked=checked;
});
//where first arg is controller unique id and second parent controller uniqueid
$scope.$watch('checked',(...notify service to change data in tree...));
当你想收集完整的树时,只需使用类似 yourService.getAll() 的东西
树形结构应该是这样的:
scope.dataTree={
'3je99ehr0jer':{checked:true,children:[ another nodes that have parentid 3je99... ]}
}
添加/更改 dataTree 使用一些递归函数来查找您需要的 id
yourService.set = function serviceSet(currentId,parentId,data,onChange){
//find recursively parentId if not null
// update value
// notify parent node (or all) controllers that something has changed if you need (onChange argument)
}
平面对象选项
如果您只想要带有 item.id->value 内容的平面对象,那么它会更简单,因为您可以在 yourService 中使用 dataTree 而不是这样的数据:
scope.data= {
'3je99ehr0jer':false,
'03kefkeo0efe':true,
...
}
现在您不需要递归查找 id,只需要从 scope.data 对象 scope.data['3je99ehr0jer'] 获取您想要的内容
我懒得测试这种方法,但你会弄清楚如何纠正一些问题,我不确定你是否需要通知 yourService $watch 方法发生了某些变化,如果 @987654329 yourService 中的 @ 方法也是需要的,但您会发现这一点,这个概念似乎是正确的 -> 基于唯一 ID 的数据交换服务
如果您发现错误,请在评论中告诉我,以便我可以编辑此答案