title: How to make directive execution deterministic in AngularJS?
body: <p>I have an interesting situation here that I have not ran into in the past and I am not sure if there is an "Angular" way of doing this natively.  Basically, I started out with a simple button like this:</p>
  
  <pre><code>&lt;button type="btn" ng-disabled="model.isDisabled"&gt;My Button&lt;/button&gt;
  </code></pre>
  
  <p>As you can see there's nothing special here except for the ngDisabled directive I am using.  I recently developed an Authorization infrastructure made up of an Angular service, a directive, and a utility service. Essentially it checks the permissions of the given user and references them with a set of required permissions in order to see if a given element should be disabled or not.  Applying this Authorization directive to my button yields the following:</p>
  
  <pre><code>&lt;button type="btn" sk-auth sk-auth-enable="[AuthPermissions.Restore]" ng-disabled="model.isDisabled"&gt;My Button&lt;/button&gt;
  </code></pre>
  
  <p>Here you will see I am simply passing in an array of permissions that the user needs in order for this button to be enabled. So if this user has the "Restore" permission, then this button should be enabled.  The issue here is that my directive and the ngDisabled directive conflict.  My directive runs and will disable the element, but then the next digest cycle will run and the ngDisabled directive will re-enable it because the expression in "ng-disabled" is falsy.</p>
  
  <p>Essentially, I need my directive to be ran first and if it determines the user CAN use this button, then it allows the other ngDisabled directive to work normally, but if it can't use the button, then the skAuth directive will disable it permanently.</p>
  
  <p>Is there a fundamental flaw in my architecture? Do I need to completely re-write this Authorization infrastructure in order to accommodate this scenario?  I originally thought of just using a method that gets called inside the ng-disabled, like <code>ng-disabled="!hasAuthAccess(AuthPermissions.Restore)"</code>, but that causes this method to be called on each digest cycle which isn't needed and we would have to start mixing Authorization logic with business logic and I would rather keep them seperated.</p>
  
  <p>Does anyone have any suggestions on how to achieve this?</p>