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) into this.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
Add a ment  | 

1 Answer 1

Reset to default 6

The 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