Basically for what I understand, Angular Change Detection will re render component if a change happened, or to be precise a property used in the template has changed. In any other case it will not (unnecessary) re render that component if the value stayed the same.
Here's the example that I've made, button inside the component triggers CD. Call of that button click event is empty so it doesn't update any value inside the component. Only this used in the template is a getter function that always returns the same simple value.
So if CD compared previous and current value there is no change so why would is seem that component re rendered since the console log inside the getter function was called.
Or it has crossed my mind, the console.log inside the getter was called during CD value comparison and the component did not really re-render.
Here's the example https://angular-playground.stackblitz.io.
import { ChangeDetectionStrategy } from '@angular/core/src/change_detection/constants';
@Component({
selector: 'my-app',
styleUrls: ['./app.component.scss'],
templateUrl: './app.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
get lastName() {
console.log('CD triggered rerender')
return 'Boodin';
}
buttonClick(){
//does nothing, doesnt set any value!
}
}
<p>{{lastName}}</p>
<button (click)="buttonClick()">Trigger Change Detection</button>