Angularjs 向现有集合添加或修改对象时,ng repeat INTERNAIN指令不会更新
我有一个treeview,其中包含我正试图填充的具有嵌套数组的对象 下面是代码结构的分解图,以及到目前为止我所做的尝试 在我的treeview.html视图中,我正在加载我创建的treeview指令: 我目前有两个测试绑定。在选中事件复选框之前,将不会填充第二个事件Angularjs 向现有集合添加或修改对象时,ng repeat INTERNAIN指令不会更新,angularjs,angularjs-scope,angular-directive,angularjs-digest,Angularjs,Angularjs Scope,Angular Directive,Angularjs Digest,我有一个treeview,其中包含我正试图填充的具有嵌套数组的对象 下面是代码结构的分解图,以及到目前为止我所做的尝试 在我的treeview.html视图中,我正在加载我创建的treeview指令: 我目前有两个测试绑定。在选中事件复选框之前,将不会填充第二个事件 <H3>Tree View Sameple</H3> <div> <h3>Treeview 1</h3> <tree src="tree.treeDa
<H3>Tree View Sameple</H3>
<div>
<h3>Treeview 1</h3>
<tree src="tree.treeData" iobj="object" filter="tree.getAggregateInfo(object, isSelected)"></tree>
<h3>Treeview 2 once object has been updated</h3>
<tree src="tree.treeData2" iobj="object" filter="tree.getAggregateInfo(object, isSelected)"></tree>
</div>
<hr/>
<p>javascript object data</p>
{{tree.treeData}}
树视图Sameple
树景1
对象更新后的Treeview 2
javascript对象数据
{{tree.treeData}}
底部绑定只是显示js文件的输出。Wihtin the treeview当您单击incident复选框时,我正在模拟正在检索的新嵌套集合,该集合应作为子元素插入到树的incident节点下
下面是treeviewController.js中使用的代码
(function () {
angular.module('app')
.controller('treeviewController', ['$log', '$scope', '$timeout', treeviewController]);
function treeviewController($log, $scope, $timeout) {
var vm = this;
//storage container for last node selected in tree
vm.lastNodeSelected = '';
vm.treeData2 = {};//test container to reflect object once updated
//original data not an arry but an object holding an array
vm.treeData =
{
"children": [
{
"name": "Document Type",
"children": [
{
"name": "Incident",
"documentType": "Document Type",
"aggregateUponField": "_ocommon.documenttype",
"parent": "",
"count": 2950,
"children": null
},
{
"name": "Some Event",
"documentType": "Document Type",
"aggregateUponField": "_ocommon.documenttype",
"parent": "",
"count": 2736,
"children": [{
"name": "Some Event Date",
"children": [
{
"name": "2008",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 451,
"children": null
},
{
"name": "2009",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 407,
"children": null
},
{
"name": "2010",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 426,
"children": null
}
]
}]
}
]
}
]
};
//aggregate data to be nested under incident
vm.newChildData = [
{
"name": "Incident Date",
"children": [
{
"name": "2008",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 451,
"children": null
},
{
"name": "2009",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 407,
"children": null
},
{
"name": "2010",
"documentType": "Incident Date",
"aggregateUponField": "dateincidentstart",
"parent": "",
"count": 426,
"children": null
}
]
}
];
var loadData = function () {
//stip out array
var children = vm.treeData.children;
//search array for matching parent
$log.info(vm.lastNodeSelected);
var cnt = children.length;
for (var i = 0; i < cnt; i++) {
if (children[i].children) {
var innerCnt = children[i].children.length;
for (var c = 0; c < innerCnt; c++) {
if (children[i].children[c].name === vm.lastNodeSelected) {
children[i].children[c].children = vm.newChildData;
}
}
}
}
//wrap back in object and assign to ngModel
vm.updatedList = { children };
// $log.info(updatedList);
vm.treeData2 = vm.updatedList;
$timeout(function() {
$scope.$apply(function() {
vm.treeData = vm.updatedList;
})} , 0);
}
vm.getAggregateInfo = function (node) {
vm.lastNodeSelected = node.name;
loadData();
// $log.info(vm.lastNodeSelected);
};
}
})();
(函数(){
角度。模块('app')
.controller('treeviewController',['$log','$scope','$timeout',treeviewController]);
函数树控件($log、$scope、$timeout){
var vm=这个;
//树中选定的最后一个节点的存储容器
vm.lastNodeSelected='';
vm.treeData2={};//测试容器以反映更新后的对象
//原始数据不是数组,而是包含数组的对象
vm.treeData=
{
“儿童”:[
{
“名称”:“文件类型”,
“儿童”:[
{
“名称”:“事件”,
“文档类型”:“文档类型”,
“aggregateUponField”:“\u ocommon.documenttype”,
“父项”:“,
“计数”:2950,
“children”:空
},
{
“名称”:“某些事件”,
“文档类型”:“文档类型”,
“aggregateUponField”:“\u ocommon.documenttype”,
“父项”:“,
“计数”:2736,
“儿童”:[{
“名称”:“某个活动日期”,
“儿童”:[
{
“名称”:“2008年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:451,
“children”:空
},
{
“名称”:“2009年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:407,
“children”:空
},
{
“名称”:“2010年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:426,
“children”:空
}
]
}]
}
]
}
]
};
//要嵌套在事件下的聚合数据
vm.newChildData=[
{
“名称”:“事件日期”,
“儿童”:[
{
“名称”:“2008年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:451,
“children”:空
},
{
“名称”:“2009年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:407,
“children”:空
},
{
“名称”:“2010年”,
“文档类型”:“事件日期”,
“aggregateUponField”:“dateincidentstart”,
“父项”:“,
“计数”:426,
“children”:空
}
]
}
];
var loadData=函数(){
//stip输出阵列
var children=vm.treeData.children;
//搜索匹配父项的数组
$log.info(vm.lastNodeSelected);
var cnt=children.length;
对于(变量i=0;iangular.module('app')
.directive('tree', function () {
return {
restrict: 'E',
replace: true,
scope: {
t: '=src',
filter: '&'
},
template: '<ul><branch ng-repeat="c in t.children track by $index" src="c" filter="filter({ object: object, isSelected: isSelected })"></branch></ul>'
};
});
angular.module('app')
.directive('branch', function ($compile) {
return {
restrict: 'E',
replace: true,
scope: {
b: '=src',
filter: '&',
checked: '=ngModel'
},
template: '<li class="dir-tree"><input type="checkbox" ng-click="innerCall()" ng-model="b.$$hashKey" ng-change="stateChanged(b.$$hashKey)" />{{ b.name }} <span ng-hide="visible"> ({{ b.count }})</span></li>',
link: function (scope, element, attrs) {
var clicked = '';
var hasChildren = angular.isArray(scope.b.children);
scope.visible = hasChildren;
if (hasChildren) {
element.append('<tree src="b" filter="filter({ object: object, isSelected: isSelected })"></tree>');
$compile(element.contents())(scope);
}
element.on('click', function (event) {
event.stopPropagation();
if (hasChildren) {
element.toggleClass('collapsed');
}
});
scope.stateChanged = function (b) {
clicked = b;
};
scope.innerCall = function () {
scope.filter({ object: scope.b, isSelected: clicked });
};
}
};
});
scope.$watch('b', function (newvalue, oldvalue) {
if (!oldvalue.children && newvalue.children) {
scope.visible = true;
element.append('<tree src="b" filter="filter({ object: object, isSelected: isSelected })"></tree>');
$compile(element.contents())(scope);
}
}, true);