异步设置时,mat-expansion-panel [expanded] 输入无法正常工作

发布于 2025-01-12 16:32:32 字数 417 浏览 3 评论 0原文

我有一个 mat-accordion,它有一个 mat-expansion-panel 设置如下:

<mat-expansion-panel *ngFor="let item of menuItems; index as i" [hideToggle]="item.hideToggle" [expanded]="item.active">

如果“item”的布尔属性“active”评估为 true,则可以看到“expanded”输入为 true。好吧,这是奇怪的部分:如果同步设置“item.active”,例如在我的组件的 ngOnInit 方法上,则一切正常并且面板打开。但是,如果“item.active”被异步设置,例如在可观察的“订阅”方法内,则面板不会打开。为什么会发生这种情况?我该如何解决?

先感谢您, 托马斯

I have a mat-accordion which has a mat-expansion-panel set like this:

<mat-expansion-panel *ngFor="let item of menuItems; index as i" [hideToggle]="item.hideToggle" [expanded]="item.active">

You can see the "expanded" input is true if "item" has the boolean property "active" evaluating as true. Well, here is the weird part: if "item.active" is set synchronously, for example on the ngOnInit method of my component, everything works and the panel opens. However, if "item.active" is set asynchronously, e.g. inside a "subscribe" method of an observable, panel does not open. Why does this happen, and how can I fix it?

Thank you in advance,
Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-01-19 16:32:32

我遇到了类似的问题,我异步加载扩展面板的内容。该问题是由于当内容通过返回对象作为标识的默认 trackByFunction 实现发生更改时,ngFor 指令重新渲染 DOM 引起的。请参阅此处

来解决这个问题

identify(index:number, item:any){
    return item.ExternalId;   // this is the unique id 
}

我通过提供自定义 trackBy 函数然后在模板中调用它

<div *ngFor="let item of items;trackBy:identify">

I had a similar problem as this where I was loading the contents of the expansion panel asynchronously. The issue is caused by ngFor directive re-rendering the DOM when the contents change via the default trackByFunction implementation that returns the object as the identity.see here.

I got around this by providing my custom trackBy function

identify(index:number, item:any){
    return item.ExternalId;   // this is the unique id 
}

then invoking it in the template

<div *ngFor="let item of items;trackBy:identify">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文