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

The onChanges callback isn't called when updating the content of a bound object #6458

Closed
templth opened this issue Jan 13, 2016 · 5 comments
Closed

Comments

@templth
Copy link

templth commented Jan 13, 2016

Hello,

Following a question on StackOverflow, I wonder why the onChanges is called when a bound object is updated (a new value) but not when the content of a bound object is updated.

Here is the following use case:

export class AppComponent implements OnChanges {
    test: { one: string; } = { one: "1" };

    ngOnChanges() {
      console.log('updated');
    }

    clicked() {
        // The ngOnChanges method is called
        this.test = { one: 3 };

        // The ngOnChanges method isn't called
        this.test.one = "4";
    }
}

Thanks for your help!
Thierry

@pkozlowski-opensource
Copy link
Member

By default change detection system does reference comparison for objects - it doesn't do deep comparison by default as it would be too costly and would negatively impact performance.

If you need to deep-observe object for changes you can do it explicitly using key-value differs. You can check implementation of the NgStyle directive for an example usage of a differ.

@templth
Copy link
Author

templth commented Jan 13, 2016

Thanks very much, Pawel! That makes sense ;-)

@templth
Copy link
Author

templth commented Jan 14, 2016

You tell "by default change detection ...". Is it possible to change this behavior in applications?

Thanks!

@mrajcok
Copy link

mrajcok commented Jan 29, 2016

Just FYI, if you want to do the explicit checks yourself, you need to implement ngDoCheck() (which is called every change detection cycle) and do the checks inside that method. (This is what the NgStyle directive example shows, but the ngDoCheck() part might not be obvious.)

Haroenv added a commit to algolia/angular-instantsearch that referenced this issue Aug 16, 2018
I took inspiration from:

- https://codesandbox.io/s/wn17j102v5
-
 angular/angular#6458
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_style.ts
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_class.ts#L44

Maybe this can be implemented with a single differ, rather than the KeyValueDiffers, but I didn't find how to

This calls `refine` on first render too, but that's okay, since it doesn't trigger an extra query

This can be tested in the dev novel

fixes #230
Haroenv added a commit to algolia/angular-instantsearch that referenced this issue Aug 16, 2018
I took inspiration from:

- https://codesandbox.io/s/wn17j102v5
-
 angular/angular#6458
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_style.ts
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_class.ts#L44

Maybe this can be implemented with a single differ, rather than the KeyValueDiffers, but I didn't find how to

This calls `refine` on first render too, but that's okay, since it doesn't trigger an extra query

This can be tested in the dev novel

fixes #230
Haroenv added a commit to algolia/angular-instantsearch that referenced this issue Aug 16, 2018
I took inspiration from:

- https://codesandbox.io/s/wn17j102v5
-
 angular/angular#6458
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_style.ts
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_class.ts#L44

Maybe this can be implemented with a single differ, rather than the KeyValueDiffers, but I didn't find how to

This calls `refine` on first render too, but that's okay, since it doesn't trigger an extra query

This can be tested in the dev novel

fixes #230
Haroenv added a commit to algolia/angular-instantsearch that referenced this issue Aug 16, 2018
* chore(tslint): set up so stuff isn't red

* docs(configure): example for a toggler with shallow change

* fix(configure): make sure deep changes are also caught

I took inspiration from:

- https://codesandbox.io/s/wn17j102v5
-
 angular/angular#6458
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_style.ts
- https://github.com/angular/angular/blob/15cc85c54a9146c6d502cffad52a3c1a41fe4dcb/packages/common/src/directives/ng_class.ts#L44

Maybe this can be implemented with a single differ, rather than the KeyValueDiffers, but I didn't find how to

This calls `refine` on first render too, but that's okay, since it doesn't trigger an extra query

This can be tested in the dev novel

fixes #230

* chore: fix linting

* chore: revert adding stuff to the config

* fix(configure): make it buildable!!!

* docs(configure): add comment

* test(configure): add test on diffing (rough)
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants