Search code examples
angularsignalsserver-side-rendering

angular signal not updating afterNextRender()


Using angular SSR howcome, I have to manually trigger a change detection if I use afterNextRender.
This is after all an angular API.

Does anyone have a solution for this?

    afterNextRender(() => {      
       this.someAPI.then((items) => {
         this.items.set(items); // <-- set signal
         this.ref.detectChanges(); // <--- trigger change detection
       });
       
    });

I do not want the API call to be made on the server side, that's why I use afterNextRender


Solution

  • afterNextRender/afterRender callbacks run outside the angular zone.

    In v17, if you want to a signal update to trigger CD, you need to do it in the angular zone.

    afterNextRender(() => {    
     this.someAPI.then((items) => {
       ngZone.run(() => {
         this.items.set(items); // <-- set signal
       });
      });
    })
    

    Note: In v18 Signals updates will trigger CD even when run outside the Angular zone