2

I found some issues with the performance regarding Change detection so I started using onPush from the past few weeks. Everything is fine till now but I donno its behaving weird with p-chips where its working only on second click :(

Stackblitz Working Example enter link description here

Here in stackblitz you can see the name changes on first click with detectChanges but thats not the scenario with p-chips... Can someone help me :)

Tried using settimeout just to check whether its refreshing total view or not but settimeout is also not updating the view

Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33
  • Well actually you shouldnt even need the explicit call for detecting changes, since the changes comes from a user click, which is handled automatically by onPush! But still weird that the values are not reflected in the chips, because if you print the values in the template, you do see that they do get emptied after you press the button the first time.. – callback Jul 03 '20 at 14:47
  • Yess donno whats wrong. The values are also getting emptied in this way - click button one time and click inside input box once and the values get cleared. – Gvs Akhil Jul 03 '20 at 14:49

3 Answers3

0

You can use setTimeout(() => this._cdRef.detectChanges(), 0); in your emptyArray function. It uses zones and detect changes after it.

Anton Marinenko
  • 2,749
  • 1
  • 10
  • 16
0

Hi guys I tried all your answers and even settimeout is not working for me. I also dont prefer using settimeout as it does change detection from app component level

This worked for me :). I really cant understand why detectChanges is working for all the rest and only for this p-chips I need to add markForCheck before it to work.

this._cdRef.markForCheck();
this._cdRef.detectChanges();
Gvs Akhil
  • 2,165
  • 2
  • 16
  • 33
-1

I believe the statement of this question will help.

I could not find a reason for this happening with OnPush detection strategy, but sending your detectChanges() call back in the queue (making it async) will solve your problem.

You can do that by calling the method in a setTimeout call:

setTimeout(() => this._cdRef.detectChanges(), 0);

Please check the stackblitz demo.

Cristian Sarghe
  • 782
  • 6
  • 15