title: Why is using if(!$scope.$$phase) $scope.$apply() an anti-pattern?
body: <p>Sometimes I need to use <code>$scope.$apply</code> in my code and sometimes it throws a "digest already in progress" error. So I started to find a way around this and found this question: <a href="https://stackoverflow.com/questions/12729122/prevent-error-digest-already-in-progress-when-calling-scope-apply">AngularJS : Prevent error $digest already in progress when calling $scope.$apply()</a>. However in the comments (and on the angular wiki) you can read:</p>
  
  <blockquote>
    <p>Don't do if (!$scope.$$phase) $scope.$apply(), it means your $scope.$apply() isn't high enough in the call stack.</p>
  </blockquote>
  
  <p>So now i have two questions:</p>
  
  <ol>
  <li>Why exactly is this an anti-pattern?</li>
  <li>How can i safely use $scope.$apply?</li>
  </ol>
  
  <p>Another "solution" to prevent "digest already in progress" error seems to be using $timeout:</p>
  
  <pre><code>$timeout(function() {
    //...
  });
  </code></pre>
  
  <p>Is that the way to go? Is it safer? So here is the real question: How I can <strong>entirely</strong> eliminate the possibility of a "digest already in progress" error?</p>
  
  <p>PS: I am only using $scope.$apply in non-angularjs callbacks that are not synchronous. (as far as I know those are situations where you must use $scope.$apply if you want your changes to be applied)</p>