-
Notifications
You must be signed in to change notification settings - Fork 27.5k
$onChanges hook is not working as expected in angular 1.5.4 #14433
Comments
The fact that The other issue that you mention, It's impossible to investigate it, though, if we can't reproduce. So, could you please create a runnable demo (e.g. using CodePen, Plnkr etc) ? |
So sorry for this one.
|
@bogdanalexe90, nothing to be sorry about 😃 There's nothing inherently better with FWIW, the required controllers should be already bound on the controller instance before calling
This seems like a real issue. It shouldn't happen imo. Could you please open a new issue about specifically ? |
|
@gkalpak I find it rather problematic as well that I rely on flags and settings in If this is happening in Angular 2 as well, then I think it's a problem there too. But at least in Angular 2 we have constructors for our components, so that can alleviate the problem a bit. However, in Angular 1 we don't have those for our directives, so we have to rely on How can you have a true initialization method, if it is not the first method that is called? |
@adambuczynski , it does happen in ng2 as well. I don't see why it is a problem, but if you convince them to change their implementation, we will probably change it in ng1 too 😛
Ehm...we do have constructors in ng1 too 😟
It is not an initialization method (it would be called BTW, if you need to, you can detect and skip the first run of |
@gkalpak While the controller function is technically a constructor, yes, various things are still inaccessible at the time of running (for example form controls). I've run into various timing issues with putting my initialisation code in these "constructors", so I resorted to using
We can argue semantics about the name of the function, but that's not the point here. The function is, and will be used by people to do initialization logic. Angular 2's guides recommend and encourage that in numerous locations. Two or three that I could quickly find:
From reading that, it most certainly seems to me that this hook is meant to be used for any kind of initialization which is heavier than setting basic scope/component properties. Anyway, I do agree that it makes sense that the first set of changes to your bound properties be available to use in the |
You can't always agree on everything, right 😃 The important thing is to agree on the stuff that matters! Just to be clear, I didn't imply that you should put your initialization logic in the construtors - I just pointed out that there are constructors in ng1 too. I agree that "your" initialization logic should be placed inside I am sure that not every usecase will be "happy" with any specific order, but hopefully the current implementation makes things easy (or at least not impossible 😛 ) the most common ones 😉 The good thing is that you won't need to rewrite your code when you migrate to ng2 😛 Thx for the conversation btw. Even if you don't reach a definitive agreement, it helps gain better understanding, especially for new concepts that are still in flux. |
Yes I agree. I think it's important for the Angular (or any framework) team to have a good understanding of user`s use cases and scenario's in order to make sure the framework will help users, not fight against them ;) |
Ya, this was a major headache for me and would have been easily resolved if there was a warning in the documentation. |
PRs improving the docs are always welcome 😉 |
+1 for |
Note: for support questions, please use one of these channels: https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#question. This repository's issues are reserved for feature requests and bug reports.
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
$onChanges
is not called every time.$onChanges
is called before$onInit
.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (template: http://plnkr.co/edit/tpl:yBpEi4).
I'm having a component with a binding defined like this:
1.The
$onChanges
is called before$onInit
hook.$onChanges
is called when the components inits, it's called for the first change and than is not called for the rest of the changes.The property that changes is an object but the changes itself is done on the object reference not on the one of the properties. This works with angular 1.5.3.
What is the expected behavior?
$onChanges
hook should be called every time when there's a change on a one-way binding property.$onChanges
should be called after$onInit
What is the motivation / use case for changing the behavior?
Not compatible with applications that worked on 1.5.3
Which versions of Angular, and which browser / OS are affected by this issue? Did this work in previous versions of Angular? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.
1.5.4 / Mac OS X 10.11.4
Other information (e.g. stacktraces, related issues, suggestions how to fix)
The text was updated successfully, but these errors were encountered: