79125321

Date: 2024-10-25 10:42:36
Score: 1.5
Natty:
Report link

As i can recall detectChanges should be trigged on: DOM events, Asynchronous tasks and Component lifecycle events, accent here is on "it should" there are cases (in my experiece at least) where angular does not trigger change detection automatically (when change is runnning outside of Angular zone) in that cases you should trigger change manually, for example using ChangeDetectorRef:

import { ChangeDetectorRef } from '@angular/core';

constructor(private cdr: ChangeDetectorRef) {}

someMethod() {
  // Code that doesn't trigger automatic change detection
  this.value = 'Updated manually';
  
  // Manually trigger change detection
  this.cdr.detectChanges();
}

but for most cases, for me, more direct way is to do what ever you need after you change value. So for given example if You are changing something onClick() and expecting some reaction to change caused by that method call another method from within onClick() which will do something yo expect with given data?

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: hajrovica