question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

detectChanges is not updating host binding with OnPush

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When using ControlValueAccessor interface and calling change detector detectChanges inside writeValue method, it is not updating host binding css class, but calling markForCheck does. Also it is important to notice that this doesn’t work only on first ngModel change, if I change it second time, then it is working fine.

In my example .has-value css class is not applied even if I call detectChanges.

  host: {
      '[class.has-value]': 'hasValue'
  },

  //...

  get hasValue() {
    return !!this.value;
  }

  writeValue(value) {
    console.log('writeValue', value);
    this.value = value;
    this._cd.detectChanges();

    // will work
    // this.cd.markForCheck();
  }

calling markForCheck will trigger view to update, but this is bad because:

  1. detectChanges should update view in any way
  2. using markForCheck in this case complicates unit testing because fixture.detectChanges should be called minimum two times because of markForCheck

Expected behavior

detectChanges should always update all bindings correctly

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-host-binding-bug?file=app%2Fapp.component.ts

Environment


Angular version: 5.x.x

Browser:
- [x] Chrome (desktop) version XX
 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
david-garcia-garciacommented, Mar 18, 2019

A workaround is to skip the current’s component ChangeDetector, this will give access to the first ancestor (in this case the host component):

  constructor(@SkipSelf() protected cdRef: ChangeDetectorRef) {
1reaction
alexzuzacommented, Mar 3, 2018

As far i know that’s by desing in Renderer2.

Calling detectChanges does not have any effect because host bindings are part of parent view. Hi view doesn’t contain expression for updating class.has-value in your example Angular will update it during executing updateRenderer function for parent view.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular. Manually updating property with HostBinding ...
First of all detectChanges inside List's ngAfterViewInit will not update Item, probably because Item is not direct child or parent of List.
Read more >
Angular OnPush Change Detection - Avoid Common Pitfalls
Scenario 1 - Our Starting Point (default change detection). Let's have a look at a simple component that does not use yet OnPush...
Read more >
Everything you need to know about change detection in ...
Here we review in great details all operations performed by Angular during change detection, explore implications and take a close look at change...
Read more >
What's The Hype With Angular's OnPush Change ...
Angular checks all components for changes on different occasions, even if some components did not change at all. These occasions include Network ...
Read more >
NgModel Host Bindings Finally Fixed But OnPush Change ...
But, the Angular 2 team is making some progress. In Beta 17 (or perhaps earlier), they seem to have fixed the host binding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found