ngOnChanges produces changes for every input, even when the objects are unchanged.
See original GitHub issueProblem: ngOnChanges
seems to always provide all inputs in the SimpleChanges object, even though some inputs are unchanged. Correct behavior would be that only the changed inputs are provided, similar to the Angular implementation.
Repro steps:
- Open stackblitz
- Click on
val1
button, which changes thesomeValue
property onHelloComponent
. - Notice in the console that the following SimpleChanges object is generated. The issue is that
name
should not come up as a change, since it has not changed.
{
"name": {
"previousValue": "World",
"currentValue": "World",
"firstChange": false
},
"someValue": {
"previousValue": "myvalue",
"currentValue": "val1",
"firstChange": false
}
}
My own debugging shows that all bound input properties are always marked as changed. I can work around it for now by checking for equality myself in the ngOnChanges
method of my component, but a fix would of course be preferable.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Detecting @Input changes in Angular with ngOnChanges ...
In this post you'll learn how to detect changes to an @Input property in Angular. We'll explore both using ngOnChanges lifecycle hook and ......
Read more >ngOnChanges only runs when the Input change comes from a ...
Hi!! ngOnChanges only runs when the Input change comes from a template binding like <component [someInput]="aValue"> . If you set it manually ...
Read more >ngOnChanges not firing for nested object - Stack Overflow
Even when Angular doesn't detect any changes to a component's input properties (using === checking), it still (by default) dirty checks all of...
Read more >Fine grained change detection with Angular | juri.dev
ngOnChanges (..) gets an object that contains every @Input property of our component as key and a SimpleChange object as according value.
Read more >Everything you need to know about the ... - InDepth.Dev
The application is designed to have a child component emitting an event and a parent component listening to this event. The event causes...
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 FreeTop 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
Top GitHub Comments
🎉 This issue has been resolved in version 7.0.2 🎉
The release is available on:
Your semantic-release bot 📦🚀
During deeper investigation is turns out that Angular’s differ works fine and library was using incorrect API to collect changes from it.
Currently new records are collected from
forEachAddedItem()
method and changed records fromforEachItem()
method which always returns all items despite any changes (which is logical). See: https://github.com/gund/ng-dynamic-component/blob/a4dd16067907df6b975b757369ae482b2bbc32c3/projects/ng-dynamic-component/src/lib/io/io.service.ts#L241-L245But there is another method
forEachChangedItem()
that returns only changed records and behaves correctly. See demo https://stackblitz.com/edit/angular-keyvalue-differ?file=src%2Fapp%2Fapp.component.tsSo I’m preparing a fix now and will release soon. Thanks again for the reported bug with reproduction demo!