I have a component with two array. arrayA and arrayB. arrayB contains filtered elements of arrayA. And in the template:
<div *ngFor="let elem of arrayB">{{elem.something}}</div>
I have a button too:
<button (click)="doSomething()">Do Something</button>
In the doSomething method I use the filter method on arrayA to update arrayB.
doSomething() {
this.arrayB = this.arrayA.filter(elem => { return ***some code***; });
}
The problem is that the view doesn't refresh. I tried to use NgZone (ngZone.run(): I wrapped the code in the method.) and ChangeDetectorRef (detectChanges()/markForCheck(): I called this at the end of the method.) too.
What should be the solution for this?
console.log(this.arrayB)at last indoSomethingfunc and see if there are actually any differencesconsole.log(arrayB), I noticed a strange thing: thedoSomething()fires twice. The first time, thearrayBis right (filtered), but the second time... it's the original. My(click)looks like this:(click)="foo && !foo.bar && doSomething()". So I guess the problems are these 'conditions'.