angular directive priority

為{幸葍}努か 提交于 2019-12-23 05:16:16

问题


I have this directive that removes a section of divs if ALL of them are empty. (expected to be an image inside)

i know that ng-repeat default priority is 1000, and that the higher values gets compiled first, so i sat the priority of the directive to be below that.

The directive:

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);
  }
 };
});

usage:

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

it works, but it really annoys me that the priority is not working, and that i have to do this timeout hack to make it work. Am i missing something ?

来源:https://stackoverflow.com/questions/36742428/angular-directive-priority

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!