【问题标题】:Why is ngif ignoring priority level?为什么 ngif 忽略优先级?
【发布时间】:2017-08-15 20:28:46
【问题描述】:

我创建了一个优先级为 1000 的自定义指令。在指令的编译函数中,我从元素中删除了 ng-if。我的假设是,由于ng-if 的优先级较低,为 600,它不应该被编译。

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>

我创建了另一个指令来检查我对优先级的理解是否正确。 myDirective 正在成功删除 myDirective1,但不是 ngIf

以下是plunker链接:

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

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-ng-if


    【解决方案1】:

    我不确定为什么优先级不起作用。我可以建议使用终端来消除较低优先级的指令。这里更新了Plunker

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

    另请参阅有关terminal 的这些问题:

    【讨论】:

    • 感谢您的回复。
    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2018-08-10
    • 1970-01-01
    • 2013-10-03
    相关资源
    最近更新 更多