Angular 6材质嵌套树不适用于动态数据 [英] Angular 6 Material Nested Tree is not working with dynamic data

查看:83
本文介绍了Angular 6材质嵌套树不适用于动态数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular 6中将mat-treemat-nested-tree-node一起使用. 我想要的是在用户切换展开图标时动态加载数据.

I am using mat-tree with mat-nested-tree-node in Angular 6. What I want is to load the data dynamically when the user toggles expand icon.

使用材料示例中给出的Flat Tree的动态数据示例对Nested Tree使用相同的概念.到目前为止,这就是我尝试过的操作 https://stackblitz.com/edit/angular-naarcp

Using the dynamic data example of Flat Tree given in Material Examples I have tried to use the same concept for Nested Tree. This is what I have tried so far https://stackblitz.com/edit/angular-naarcp

但是它仅显示数据数组中预填充的数据,尽管在控制台中很明显数据正在更新,但从未显示在UI上.

But it only shows the data which was prepopulated in the data array although in the console it is clear that data is getting updated but it never gets shown on the UI.

它为节点parent, child1, child2, child3递归调用_getChildren方法,因为这是初始数据.当用户展开它时,我在child1child3中添加了My Child,但从未显示添加的节点.

It recursively calls the _getChildren method for the nodes parent, child1, child2, child3 because this is initial data. I am adding My Child in child1 and child3 when user expands it but the added node is never shown.

我无法在_getChildren中添加动态子级,因为它会递归地调用到最后一个节点.

I can not add dynamic children in _getChildren because it gets called recursively till last node.

我不想使用Flat tree,因为它可以管理单个数组中的所有内容,并且在异步加载数据时更新单个数组真的很困难

I dont want to use Flat tree because it manages everything in single array and updating the single array gets really difficult in asynchronous loading of data

有什么我想念的东西吗?还是嵌套树被设计为以这种方式工作?

Is there anything I am missing or nested trees are designed to work this way?

推荐答案

当我第一次实现该实现时,我感到很挣扎,发现UI没有更新,因为对对象属性的更改不会被更改检测所吸收.请仔细阅读我的原始问题并在此处回答.它是用于平整的树,但可以节省您数小时的敲打头步.

I struggled when I implemented back when it first came out and found the UI wasnt updating because changes to an objects's properties do not get picked up by change detection. Please read through my original question and answer here. It is for a flattened tree but might save you hours of banging your head.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆