detectChanges is not updating host binding with OnPush
See original GitHub issueI’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:
- detectChanges should update view in any way
- 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:
- Created 6 years ago
- Reactions:4
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A workaround is to skip the current’s component ChangeDetector, this will give access to the first ancestor (in this case the host component):
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 updatingclass.has-value
in your example Angular will update it during executingupdateRenderer
function for parent view.