无法折叠/展开材质树(Angular 6)
发布时间:2020-12-17 17:11:57 所属栏目:安全 来源:网络整理
导读:我正在尝试在我的应用中设置Material Tree.与mat-table f.ex.相比,这相当复杂,但我设法获取数据并显示出来.但是,我只能展示所有名字,而不是扩展/折叠他们的孩子.我收到了错误 cannot read property treeControl of undefined 点击项目时.我尝试了另一个SO帖
我正在尝试在我的应用中设置Material Tree.与mat-table f.ex.相比,这相当复杂,但我设法获取数据并显示出来.但是,我只能展示所有名字,而不是扩展/折叠他们的孩子.我收到了错误
cannot read property treeControl of undefined 点击项目时.我尝试了另一个SO帖子的建议,然后我得到了一个额外的错误 cannot read property length of undefined 我该如何解决这个问题?这是我现在的代码: overview.component.html <mat-tree [dataSource]="nestedDataSource" [treeControl]="nestedTreeControl" class="example-tree"> <mat-tree-node *matTreeNodeDef="let node"> <li class="mat-tree-node"> <button mat-icon-button disabled></button> {{ node.name }} </li> </mat-tree-node> <mat-nested-tree-node *matTreeNodeDef="let node; when: hasNestedChild"> <li> <div class="mat-tree-node"> <button mat-icon-button matTreeNodeToggle [attr.aria-label]="'toggle ' + node.name"> <mat-icon class="mat-icon-rtl-mirror"> {{nestedTreeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}} </mat-icon> </button> {{node.name}} </div> <ul [class.example-tree-invisible]="!nestedTreeControl.isExpanded(node)"> <ng-container matTreeNodeOutlet></ng-container> </ul> </li> </mat-nested-tree-node> </mat-tree> overview.component.scss example-tree-invisible {display: none;} .example-tree ul,.example-tree li { margin-top: 0; margin-bottom: 0; list-style-type: none; } overview.module.ts import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { OverviewComponent } from './overview.component'; import { MatTreeModule,MatIconModule,MatButtonModule } from '@angular/material'; @NgModule({ imports: [ CommonModule,MatTreeModule,MatButtonModule ],declarations: [OverviewComponent] }) export class OverviewModule { } overview.component.ts import { Component,OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { MatTreeNestedDataSource } from '@angular/material'; import { NestedTreeControl } from '@angular/cdk/tree'; import { of as observableOf } from 'rxjs'; export class Group { name: string; } export class MasterGroup extends Group { childGroups: Array<Group>; } @Component({ selector: 'app-overview',templateUrl: './overview.component.html',styleUrls: ['./overview.component.scss'] }) export class OverviewComponent implements OnInit { nestedTreeControl: NestedTreeControl<Group>; nestedDataSource: MatTreeNestedDataSource<MasterGroup>; constructor(private http: HttpClient) { this.nestedTreeControl = new NestedTreeControl<Group>(this._getChildren); this.nestedDataSource = new MatTreeNestedDataSource(); this.getGroups(); } ngOnInit() { } private _getChildren = (node: MasterGroup) => { return observableOf(node.childGroups); } getGroups() { const data = [ { name: 'test',childGroups: [ { name: 'inner test' },{ name: 'inner test 2' } ] },{ name: 'test 2',childGroups: [ { name: 'another test' } ] },{ name: 'test 3',childGroups: [] } ]; this.nestedDataSource.data = data; } hasNestedChild = (_: number,nodeData: MasterGroup) => { /*if (nodeData.childGroups) { // I get an error using .length here - without it shows all groups // When clicking item it gives error 'cannt read property treeControl of undefined' return true; } else { return false; } */ // tried this line from another SO-post,I then get both errors at same time return nodeData.childGroups.length; } } 解决方法
好的,虽然我已经有了Angular 7(手动升级),但我遇到了同样的问题.
为了解决这个问题,我创建了一个新的Angular项目(使用ng new),并确保package.json中的所有包版本都与我的项目相匹配. 然后它奏效了.我想有一些依赖项,比如@ angular / cdk,与我以前的tsconfig.json不兼容. 我希望有人来这里,也有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- angularjs – 无法运行量角器 – ECONNREFUSED连接ECONNREF
- twitter-bootstrap – 导航链接到部分 – 由navbar-fixed-t
- AngularJS. ui-view包含在ng-view中
- bash – 如何输入’y’而不必按ENTER键?
- bootstrap-不良效果层叠进度条
- BootStrap点击下拉菜单项后显示一个新的输入框实现代码
- Axis2发布WebService例子(HelloWorld)
- bootstrap-treeview自定义双击事件实现方法
- CXF、Restlet、RESTEasy、Jersey对比
- 为什么Split在不同的字符串上表现不同?