Trying zoneless experimental feature in Angular, when updating a signal value, I would like to re-render only the views related to the signal.
Given this example :
@Component({
selector: 'app-root',
standalone: true,
imports: [ChildComponent],
template: `
<div>{{ getDate() }}</div>
<app-child></app-child>
`
})
export class AppComponent {
getDate(): string {
return new Date().toString();
}
}
@Component({
selector: 'app-child',
standalone: true,
template: `
<div>{{ getDate() }}</div>
<button type="button" (click)="reset()"></button>
<div>
{{ value() }}
</div>
`
})
export class ChildComponent implements OnInit {
value = signal(0);
ngOnInit(): void {
setInterval(() => {
this.value.update((v) => v + 1);
}, 1000);
}
reset() {
this.value.set(0);
}
getDate(): string {
return new Date().toString();
}
}
I get the expected behavior when updating the value in setInterval
. Only the child component view is refreshed (the printed date in AppComponent
doesn’t change).
When I click on the reset button, a change detection is triggered over the whole app (so the printed date in AppComponent
is updated).
As I only update a signal value, I would like to prevent this change detection and only re-render the ChildComponent
view.
Is there a way to prevent this change detection cycle for template listeners like click
, mouseup
, mousedown
etc. when running Angular in zoneless mode ?