2

I am working on an enterprise Angular application, with Ruby PageObject for testing. The problem we are running into is that sometimes an $interval that we have kicks off the $digest leading to random failures. I am trying to write a Javscript command that the page object can execute to see if a $digest is running. To do this I came up with the following...

angular.element("#application").injector().get('$rootScope').$$phase

This seems to work, however, I wanted to create a plunker to demonstrate the issue, however, this plunker doesn't change from $digest. However when I add the following....

$timeout(function() {
      console.log("phase is "+$scope.$root.$$phase);
})

I see it become null, so how can I show that in the UI?

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • 1
    Why are you forcing a $digest within a $interval? You should only need to do a $apply when you know something has changed outside of the angular life cycle. Also all values that start with `$$` are internal values are should never be used in a live application. – Enzey Apr 22 '15 at 17:07
  • 1
    A watcher will always be called in the same phase. So the value will always be the same. – a better oliver Apr 22 '15 at 17:31
  • @Enzey huh? the timeout isn't forcing a digest it is waiting till the digest is complete. That is why the phase is null inside the timeout. As for the $$ this is inside the test not the application itself the test grabs the DOM element and uses the injector from it to grab the $rootScope. This would not be anything included in the final source – Jackie Apr 22 '15 at 18:28
  • @zeroflagL so I guess you have technically answered my question when I move it outside of the controller (http://plnkr.co/edit/8H6gYJ9XlwVFM2SEujtB?p=preview) it seems to preform like I would expect. Is there anyway I can do it from the controller itself? – Jackie Apr 22 '15 at 19:49
  • @Jackie you are doing it in hacky way by adding `area.innerHTML` – Pankaj Parkar Apr 22 '15 at 19:57
  • @pankajparkar that is the point I can't get it to work without being hacky. I think because everytime the compile happens is during the actual $apply phase so the text doesn't change till you are out of the scope. – Jackie Apr 23 '15 at 13:22

1 Answers1

0

AFAIK You can show $root.$$phase value on UI, but it would be always shown as $digest only, it will never shown as null. Because Angular does run digest cycle for each 50ms to update all data-bindings, you need to look at Databinding in AngularJS.

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Thank you but the situation isn't to update data bindings it is to tell a separate (ruby test suite in our case) test application when they are occurring to avoid stale state exceptions. Also since the digest cycle only takes 50ms why don't I see it go null instead of see it get stuck on $apply. Please play around with the plnkr to try to derive what I am refering to (basically after the button click it turns null I want it to be like that after it has started as well). – Jackie Apr 22 '15 at 19:49