角度指令优先级

时间:2016-04-20 11:29:01

标签: angularjs angularjs-directive

我有这个指令,如果所有div都为空,则删除一部分div。 (预计是里面的图像)

我知道ng-repeat默认优先级是1000,并且更高的值首先被编译,所以我将指令的优先级设置为低于该值。

指令:

myApp.directive('imageContainerRemoval', function() {
 return {
  priority: 999,
  restrict: 'E',
  link: function postLink (scope, element, attrs) {
   var watchGroup = attrs.imageContainerRemoval,
       sectionGotImage = false;

    // ** WORK AROUND FOR PRIORITY! **
    // 0 milisec delay to ensure ng-style is applied and ng-repeat has finished.
    setTimeout(function() {
    var elementsInClass = element.find('div[image-container-removal="' + watchGroup + '"]');

    // Check for images in section.
    if (elementsInClass.children().context.children.length > 0) {
      sectionGotImage = true;
    }

    // If there's no images in section, delete all image container divs.
    if (!sectionGotImage) {
      $(elementsInClass.children().context).remove();
      sectionGotImage = false;
    }
   }, 0);
  }
 };
});

用法:

<div class="imageContainer" image-container-removal="chart-{{Id}}">
<img class="thumbnailIMG" ng-src="{{::data.image}}" ng-if="data.image != null" />
</div>

它的工作原理,但它确实让我感到恼火,优先级不起作用,我必须做这个超时黑客才能使它工作。我错过了什么吗?

0 个答案:

没有答案