我有一个指令。该指令处理子元素的拖放。子元素的列表将由指令从给定的数据数组生成。为了渲染每个元素,我想给指令一个渲染。这时候我使用了一个健身房。但我认为这不是过滤器的工作。有没有办法为它使用其他指令?
指令:
module.directive('dndSortable', ['$filter', '$log',
function ($filter, $log) {
return {
restrict: 'A',
scope: {
dndValues: '=',
dndRenderFilter: '@'
},
link: function (scope, element) {
angular.forEach(dndValues, function(value){
//my first idea was to use a fiter this will create a element
//but I think thats not the function of fiters
//maybe there is a way to use a directive here
var dndElement = $filter(scope.dndRenderFilter)(value);
// ... add event handling for dnd to the dndElement
});
}
}
}]);
过滤器:
module.filter('mySortListRenderFilter', function () {
return function (imageValue) {
var img,
prefix = 'http://prefix/url';
if (!angular.isUndefined(imageValue) || !imageValue.hasOwnProperty('scr')) {
img = angular.element('<img>');
img.attr('src', prefix + imageValue.src);
img.addClass('teaser-gallery-image');
}
return img;
};
});
HTML:
<div
dndSortable
dnd-values="mySortableList"
dnd-render-filter="mySortListRenderFilter">
</div>