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.

ngOnChanges produces changes for every input, even when the objects are unchanged.

See original GitHub issue

Problem: 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 the someValue property on HelloComponent.
  • 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:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gundcommented, Oct 14, 2020

🎉 This issue has been resolved in version 7.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

1reaction
gundcommented, Oct 14, 2020

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 from forEachItem() 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-L245

But 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.ts

So I’m preparing a fix now and will release soon. Thanks again for the reported bug with reproduction demo!

Read more comments on GitHub >

github_iconTop 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 >

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