我正在努力在Angular中创建一个模型。该模型基本上是父子关系,如ul =>李的关系 我已成功将父JSON数组绑定到$ scope.parents变量,并且我可以对父变量进行重复操作。 但是,当我从承诺的结果中将子项添加到$ scope.parents.children时,我在视图中什么也得不到。
我的控制器代码如下:
departmentModule.controller("departmentController", function ($scope, departmentService) {
$scope.parents = [];
$scope.tempArr = [];
//Load initial root/parent structure
departmentService.getParents(getSelectedEventId()).then(function (result) {
$scope.parents = result;
$scope.bindChildNodes()
});
//loop through parent structure and get child nodes 1 level deep for initial load
$scope.bindChildNodes = function () {
for (var i = 0; i < $scope.parents.length; i++) {
var assignmentId = $scope.parents[i].ID;
$scope.parents[i].children = [];
departmentService.getChildren(getSelectedEventId(), assignmentId).then(function (result) {
//i would send result to children here, but this function doesnt have access to the i var to know what parent the children belong too..
$scope.tempArr = result;
});
$scope.parents[i].children = $scope.tempArr;
}
}
});
当我将$ scope.tempArr分配给其父级的children属性时:$ scope.parents [i] .children它以某种方式丢失了它的值 - 所以$ scope.parents [0] .children的console.log是一个空数组。 有这个想法或帮助吗?为什么我不能将JSON对象分配给范围变量的子属性? Simple Plunkr simulation is here 非常感谢提前。