What is `priority` of ng-repeat directive can you change it?

后端 未结 2 1093
我寻月下人不归
我寻月下人不归 2020-12-01 07:51

Angular Documentation says: -

The compilation of the DOM is performed by the call to the $compile() method. The method traverses the DOM and matches

2条回答
  •  自闭症患者
    2020-12-01 08:04

    AngularJS finds all directives associated with an element and processes it. This option tells angular to sort directives by priority so a directive having higher priority will be compiled or linked before others. The reason for having this option is that we can perform conditional check on the output of the previous directive compiled.

    In the followed example, first add button and only after add class to current button:

    Demo Fiddle

    App.directive('btn', function() {
      return {
        restrict: 'A',
        priority: 1,
        link: function(scope, element, attrs) {
          element.addClass('btn');
        }
      };
    });
    
    App.directive('primary', function() {
      return {
        restrict: 'A',
        priority: 0,
        link: function(scope, element, attrs) {
          if (element.hasClass('btn')) {
            element.addClass('btn-primary');
          }
        }
      };
    });
    

提交回复
热议问题