我试图在用户展开节点时动态加载子节点。
问题是当我填充子数组时,mat-tree 不显示子数组。如果我使用简单的 *ngFor 显示相同的数据,当子数组添加了元素时,它会显示它们。
我这里有一个工作示例:stackblitz example 这是代码和html
ts
import {NestedTreeControl} from '@angular/cdk/tree';
import {Component} from '@angular/core';
import {MatTreeNestedDataSource} from '@angular/material/tree';
export class PropertyLevel {
constructor(
public code : string,
public hasSubLevels: boolean,
public subproperties : PropertyLevel[]
){}
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
nestedTreeControl: NestedTreeControl<PropertyLevel>;
nestedDataSource: MatTreeNestedDataSource<PropertyLevel>;
constructor() {
this.nestedTreeControl = new NestedTreeControl<PropertyLevel>(this._getChildren);
this.nestedDataSource = new MatTreeNestedDataSource();
this.nestedDataSource.data = [
new PropertyLevel( '123', false, []),
new …Run Code Online (Sandbox Code Playgroud)