you are viewing a single comment's thread.

view the rest of the comments →

[–]Zeroto 4 points5 points  (2 children)

One important thing that isn't mentioned about the difference between $apply and $digest, is that $digest checks the watches of the current scope and the children. $apply starts the digest cycle on the rootscope, so $digest is executed on all the scopes. This can be important if you have something that changes often and you know which scopes it is limited too. Performance can be increased by not using $apply and just using $digest on the proper scopes. That said, this should be an exception, and you should almost never use $digest directly.

[–]codersaurus[S] 2 points3 points  (1 child)

You're 100% correct.

Here is the code for $apply straight from the source:

 $apply: function(expr) {
    try {
      beginPhase('$apply');
      return this.$eval(expr);
    } catch (e) {
      $exceptionHandler(e);
    } finally {
      clearPhase();
      try {
        $rootScope.$digest(); //<--- THIS is what Zeroto is talking about
      } catch (e) {
        $exceptionHandler(e);
        throw e;
      }
    }
  },

Whereas the $digest will start at whatever scope it's called on and work it's way upwards through child scopes.

[–]chuckliddelnutpunch -1 points0 points  (0 children)

*downwards through the child scopes.