Update a variable of component in the callback function of cdkObserveContent directive. The component template will not be updated. No debounce setting, it should not runoutsideAngular?
For example,
Template snippet:
<h2 (cdkObserveContent)="_onContentChange()">Here are some links to help you start: {{variable}} </h2>
<ul [class.has-bgColor]="_hasBgColor">
......
</ul>
<button (click)="_onToggleButtonClick2()">Touch Label</button>
Component snippet:
_hasBgColor = true;
variable = 1;
_onContentChange() {
this._hasBgColor = !this._hasBgColor;
this._changeDetectorRef.markForCheck();
}
_onToggleButtonClick2() {
this.variable += 1;
}
CD should be triggered and template should be updated.
I try it with stackblitz, but it has issue.
I want to know what can or can't do in cdkObserveContent callback.
Ha, @mmalerba literally just ran into this yesterday and we decided not to do anything about it unless someone filed an issue since it causes problems inside Google to make it emit inside the zone.
as a workaround you can inject the NgZone and do:
```ts
constructor(private _zone: NgZone, private _changeDetectorRef: ChangeDetectorRef) {}
_onContentChange() {
this._zone.run(() => {
this._hasBgColor = !this._hasBgColor;
this._changeDetectorRef.markForCheck();
});
}
Most helpful comment
as a workaround you can inject the
NgZoneand do:```ts
constructor(private _zone: NgZone, private _changeDetectorRef: ChangeDetectorRef) {}
_onContentChange() {
this._zone.run(() => {
this._hasBgColor = !this._hasBgColor;
this._changeDetectorRef.markForCheck();
});
}