【发布时间】:2018-12-23 09:30:35
【问题描述】:
我正在开发一个项目列表。使用我的 html 文件中的循环获取每个项目并将其放置在 mat-expansion-panel 中。
我想要做的是:如果某些项目的 id 被传递到 URL(例如:example.com/items/id),那么该 id 的扩展面板 将仅展开,如果未找到该项目,则它将查找该项目的数据库并将其推送到当前项目列表中,并且将再次调用相同的过程,这一次将找到并扩展该项目。
我目前的做法与这个逻辑类似。
我的 .HTML 文件:
<mat-accordion>
<mat-expansion-panel *ngFor="let item of items" [expanded]="">
// Content
</mat-expansion-pane>
</mat-accordion>
我的 Component.ts 文件:
expandItem(id: string) {
this.getAllItems(); // calling list of all items and saving it into 'items' variable.
for (const item of this.items) {
if (item.id === id) {
console.log('Found item into the list');
// expand that expansion panel somehow -_-'
} else {
console.log('not found in the list');
// Now fetch that item from Database.
this.FunctionService.getItem(id).subscribe((data: {}) => {
this.item = data;
}, (err) => {
console.log(err);
});
if (this.item) {
// if found then push this item inside the list and call this function again so that this time item will be found and gets expanded.
this.items.push(this.item);
this.expandFunction(id);
}
}
}
}
ngOnInit() {
this.route.params.subscribe((params) => {
if (params['id']) {
this.expandItem(params['id']); // call function which contains the expand panel code
} else {
this.getAllItems(); // otherwise call all items
}
});
}
【问题讨论】:
标签: angular typescript angular-material-6