admin管理员组文章数量:1323714
I am trying to use the async
pipe with *ngFor
to display an array of items acquired asynchronously.
<ul>
<li *ngFor="let item of items | async; trackBy: trackPost">
{{item.text}}
</li>
</ul>
ngOnInit() {
// very simple http call; returns an array of [{id: 1, text: "something"}]
this.items = this.itemService.loadItems();
}
trackPost(index, item) { return item.id; }
This works fine. Then I want to add an item:
async addItem(text) {
await this.itemService.addItem({text});
// reload items after adding
this.items = this.itemService.loadItems();
}
This also works, and it will update the items properly after it has been added.
The problem is that it will reload the entire array rather than just appending items. You can notice this with animations (if you animate items in). I know that I can handle the subscription on my own and work with an array, but I am wondering if there is a way to do this with the async pipe.
Is there a way for me to add the new item onto the existing observable? Failing that, is there a way to have the template properly track the items rather than think of them as being re-added?
I am trying to use the async
pipe with *ngFor
to display an array of items acquired asynchronously.
<ul>
<li *ngFor="let item of items | async; trackBy: trackPost">
{{item.text}}
</li>
</ul>
ngOnInit() {
// very simple http call; returns an array of [{id: 1, text: "something"}]
this.items = this.itemService.loadItems();
}
trackPost(index, item) { return item.id; }
This works fine. Then I want to add an item:
async addItem(text) {
await this.itemService.addItem({text});
// reload items after adding
this.items = this.itemService.loadItems();
}
This also works, and it will update the items properly after it has been added.
The problem is that it will reload the entire array rather than just appending items. You can notice this with animations (if you animate items in). I know that I can handle the subscription on my own and work with an array, but I am wondering if there is a way to do this with the async pipe.
Is there a way for me to add the new item onto the existing observable? Failing that, is there a way to have the template properly track the items rather than think of them as being re-added?
Share Improve this question edited Jun 28, 2017 at 14:54 Explosion Pills asked Jun 28, 2017 at 14:20 Explosion PillsExplosion Pills 192k55 gold badges340 silver badges416 bronze badges 3- Facing the same issue, having algolia search ... – Emil Iakoupov Commented Jan 30, 2018 at 3:01
-
1
Will angular not notice if you'd just
push(newItem)
intothis.items
? – JJWesterkamp Commented Feb 4, 2018 at 21:13 - Anyone figured out what's happening with that and any workaround? Thanks! – Daniel Valls Estella Commented Apr 30, 2018 at 15:16
1 Answer
Reset to default 6The problem is that you reassign the stream via this.items = this.itemService.loadItems();
. So your Observable items
does not emit new values, which Angular would track with your trackBy funciton, rather than you change the reference of the items
, causing angular to do a full reload.
So just change your addItem
-function of your service to emit the updated values to your previously gotten Observable via loadItems
. After that you simply call
addItem(text) {
this.itemService.addItem({text});
}
Example of a service:
@Injectable()
export class Service {
private items: Array<string> = [];
private items$ = new Subject<Array<string>>();
loadItems(): Observable<Array<string>> {
return this.items$.asObservable();
}
addItem(text: string) {
this.items = [...this.items, text];
this.items$.next(this.items);
}
}
本文标签: javascripttrackBy with the async pipeStack Overflow
版权声明:本文标题:javascript - trackBy with the async pipe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742128281a2422042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论