Angular *ngFor TrackBy Element 超过 2 级循环
我的代码中有 2 个 *ngFor 循环,有点像这样:
<div *ngFor="let page of pages; trackBy: trackByPageId" class="page">
<app-item-component *ngFor="let item of page; trackBy: trackByItemID"
[item]="item">
</app-item-component>
</div>
现在项目可以从一个页面移动到另一个页面,并且我不希望应用程序项目组件在页面之间移动时重新初始化。
使用 Angular 的 trackBy 允许移动数组项目,而无需再次运行 ngOnDestroy 和 ngOnInit 的组件,但是一旦我使用 2 个嵌套循环并且项目从一个列表移动到另一个列表,这就不再起作用了。
例如,像这样移动数组中的项目:页[0]项[0] ->页[1]项[0]
将导致项目组件重新初始化。
有办法解决这个问题吗?
I have 2 *ngFor loops in my code somewhat like this:
<div *ngFor="let page of pages; trackBy: trackByPageId" class="page">
<app-item-component *ngFor="let item of page; trackBy: trackByItemID"
[item]="item">
</app-item-component>
</div>
Now the items may be moved from one page to another and i do not want the app-item Component to re-initialize when i move it between pages.
Using angular's trackBy allows array items to be moved without the components running ngOnDestroy ang ngOnInit again, but as soon as im using 2 nested loops and an item moves from one list to another, this doesn't work anymore.
For Example, moving an item in the array like this:pages[0]items[0] -> pages[1]items[0]
would cause the item component to re-initialize.
Is there a way to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在.ts内部有1个输入属性
,并在.html内使用它,
以通过容器NGIF渲染。
You can have 1 Input property inside your .ts
and use it inside .html
This way it will render depending on the containers ngIf