I have a list that binds two arrays together with an ngfor using pipes. i bind the arrays on a typeID.

One array lists all the available types and the other holds the selected types along with an extra option. Note The arrays only share 2 properties, name and id


Because my ngFor is bound to the first array when data changes in the second array nothing is updated so at the minute i have to recall my http method which grabs the exact same data just to update the list.

I need a way to force rerender without calling my api everytime something in the second array changes



4 Answers

Community On Best Solutions

Using a tweaked version of onik's solution i managed to solve this with less lines of code


this.secondary = this.secondary.concat({ primaryID: primary.primaryID, name: primary.name, isChecked: false });



abd995 On

You can use ngIf and a setTimeout to make the ngFor re render. Call this.reload() instead of this.getPrimary()

reload() {
  this.show = false;
  setTimeout(() => this.show = true);

Wrap the ngFor inside a ngIf

<ul class="list list-a" *ngIf="show">
   <li *ngFor="let p of primary;">

Plunker : https://stackblitz.com/edit/angular-geuesx

Richard Fox On

you can import the ChangeDetectorRef from the angular core and trigger markForCheck. This is probably not the optimal way, but it should re-render the component in your case

onik On

you can set for new reference on this array and it will rerender,

this.secondary = [...this.secondary];