我正在用angularJs中的複選框選項製作嵌套樹結構。Angularjs:具有相同對象數據的2個不同範圍共享相同的行爲
從後端我的對象如下。
{
"2": {
"id": 2,
"name": "Under Graduate",
"slug": "under-graduate",
"parent_id": 0,
"level": 0,
"path": "0",
"show": false,
"selected": false,
"children": {
"3": {
"id": 3,
"name": "B.Com",
"slug": "b-com",
"parent_id": 2,
"level": 1,
"path": "2",
"show": false,
"selected": false,
"children": {
"4": {
"id": 4,
"name": "Commerce",
"slug": "commerce",
"parent_id": 3,
"level": 2,
"path": "2\/3",
"show": false,
"selected": false
}
}
}
}
}
}
我已經與李元件內部
$scope.a = object;
$scope.b = object;
現在我已經使用納克 - 重複分配相同的對象2 $範圍變量具有不同名稱的NG-包括如下
<li ng-repeat="a in a.children" ng-include="'a_tree_renderer.html'"></li>
使用相同的方法w ith另一個變量(b)
<li ng-repeat="b in b.children" ng-include="'b_tree_renderer.html'"></li>
在這兩個模板中,它們是擴展和查看所有子項的選項。
所有與樹算法有關的工作都很好,除非我在一個模板前執行此操作。 b,不知怎的,模板(a)的索引也會發生變化,即如果從第二個模板展開一個選項,第一個模板上的同一個事物就會展開,反之亦然。
知道您提到的「操作」對範圍變量有什麼影響會有所幫助。 – austinbruch
say $ scope.a.show&$ scope.b.show等於false,當我更改$ scope.b.show = true時,它也將$ scope.a.show更改爲true。 –
'$ scope.a'和'$ scope.b'引用同一個對象,所以對其中任何一個的更改都會影響到另一個。如果你希望他們是獨立的,你可能想要調查[angular.copy](https://docs.angularjs.org/api/ng/function/angular.copy) – austinbruch