Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: cdkObserveContent callback does not trigger change detection correctly #11280

Open
jianliao opened this issue May 11, 2018 · 2 comments
Open
Labels
area: cdk/observers needs: discussion Further discussion with the team is needed before proceeding P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@jianliao
Copy link
Contributor

jianliao commented May 11, 2018

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?

@jelbourn jelbourn added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent discussion labels May 11, 2018
@jelbourn
Copy link
Member

jelbourn commented May 11, 2018

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.

@mmalerba
Copy link
Contributor

as a workaround you can inject the NgZone and do:

constructor(private _zone: NgZone, private _changeDetectorRef: ChangeDetectorRef) {}

_onContentChange() {
  this._zone.run(() => {
    this._hasBgColor = !this._hasBgColor;
    this._changeDetectorRef.markForCheck();
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: cdk/observers needs: discussion Further discussion with the team is needed before proceeding P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

4 participants