4

I have created a custom directive with a priority of 1000. In the directive's compile function I am removing ng-if from the element. My assumption is that since ng-if has a lower priority of 600, it should not get compiled.

app.js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

});
app.directive('myDirective', function(){
  return {
    priority: 1000,
    compile: function(element){
      angular.element(element).removeAttr('ng-if').removeAttr('my-directive1');
    }
  };
});
app.directive('myDirective1', function(){
  return {
    compile: function(){
      console.log('in mydirective1');
    }
  };
});

index.html

<div my-directive ng-if="false" my-directive1>
  This div should be visible.
</div>

I have created another directive to check if my understanding of priority is correct. myDirective is successfully removing myDirective1, but not ngIf.

Following is the plunker link:

https://plnkr.co/edit/86mauwbt5I2aV4aoySpz?p=preview

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Vaibhav
  • 569
  • 6
  • 31

1 Answers1

2

I'm not sure why priorities does not work that way. I can suggest using terminal to eliminate lower priority directives. Here is updated Plunker.

app.directive('myDirective', function(){
  return {
    priority: 1000,
    terminal: true,
    compile: function(element){
      //element.removeAttr('ng-if').removeAttr('my-directive1');
    }
  };
});

Also see these questions about terminal:

Community
  • 1
  • 1
Kursad Gulseven
  • 1,978
  • 1
  • 24
  • 26