Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$scope.$watch doesn't see DOM changes done in current $digest cycle, but sees them in next cycle #10522

Closed
olexme opened this issue Dec 19, 2014 · 5 comments
Milestone

Comments

@olexme
Copy link
Contributor

olexme commented Dec 19, 2014

Specifically it doesn't see changes made for class attribute by ng-class directive.
The issue occures for AngularJS v1.3.7, for Angular 1.2.26 is ok.
Below is easy to analyse fure Angular plunker example, with $watch based on function checking class attribute changes in each digest cycle (or more times per digest).
Steps to reproduce the issue:

  1. click checkbox to apply "redClass" style on Foo Paragraph (it becomes red)
  2. click "No Operation" button to poke next digest cycle and observe that $watch now "sees" applied redClass
  3. switch to Angular 1.2.26 to see the difference in behaviour

http://plnkr.co/edit/sTjGayZnG8ti6X6LgElZ?p=preview

@pkozlowski-opensource
Copy link
Member

@olexme putting aside behavior change between 1.2.x and 1.3.x you should never ever watch DOM properties - accessing DOM in the detection function of the $watch is a big perf no-no.

I'm not sure what is your real-life use-case but I don't think we want to investigate / fix the issue you've exposed as this is not a use-case we want to promote in AngularJS. If you could talk about a real-life problem you are trying to solve, maybe someone will be able to provide a more AngularJS-like solution.

@olexme
Copy link
Contributor Author

olexme commented Dec 19, 2014

Thanks for that comment.
It's something that I also don't like and I'm aware of its ugly solution at least from performance perspective.
That approach was used to deal with 3rd party non-angular component but I know easy workaround.

I added this issue cause I didn't find explicit breaking change note in migration guide

@gkalpak
Copy link
Member

gkalpak commented Dec 19, 2014

Without looking deep into it, it must be a change to ngAnimate in v1.3.0-rc.5, for which there is a BREAKING CHANGE notice in the Changelog:

ngAnimate: defer DOM operations for changing classes to postDigest (667183a, #8234, #9263)

So, technically speaking, it is a change that happened in 1.3.x; e.g. v1.3.0-rc.4 does work the same as 1.2.x.

@olexme: It is always a good idea to check the Changelog for breaking changes when upgrading to a newer version.

@gkalpak
Copy link
Member

gkalpak commented Dec 19, 2014

Hm...looking at it again, it seems that the Changelog notice is somewhat misleading:
It seems to imply that the changes apply to ngAnimate, but actually they apply to Angular's core $animate service used when ngAnimate is not present (which is clearly explained in the relevant commit's message).

@pkozlowski-opensource pkozlowski-opensource added this to the Purgatory milestone Dec 19, 2014
@pkozlowski-opensource
Copy link
Member

Yeh, OK, it sounds like a documented breaking change. Anyway, as stated above, this is really a corner case with the behaviour that definitively shouldn't be encouraged.

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