Components: Bug: cdkObserveContent callback does not trigger change detection correctly

Created on 11 May 2018  路  2Comments  路  Source: angular/components

Bug, feature request, or proposal:

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;
}

What is the expected behavior?

CD should be triggered and template should be updated.

What is the current behavior?

What are the steps to reproduce?

I try it with stackblitz, but it has issue.

What is the use-case or motivation for changing an existing behavior?

I want to know what can or can't do in cdkObserveContent callback.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Is there anything else we should know?

P3 cdobservers needs discussion

Most helpful comment

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();
});
}

All 2 comments

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();
});
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

julianobrasil picture julianobrasil  路  3Comments

3mp3ri0r picture 3mp3ri0r  路  3Comments

savaryt picture savaryt  路  3Comments

crutchcorn picture crutchcorn  路  3Comments

vanor89 picture vanor89  路  3Comments