问题
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