Tao Wang Tao Wang - 3 months ago 10
AngularJS Question

AngularJS directives how to get deeper tree nodes

My html view looks like below:

<div mydirectives="options..." class='container'>
<div class='wrapper'>
<img src='{{data.img}}' class='slide' ng-repeat='..'/>
</div>
</div>


My question is how I'm able to get slide objects in directives?
What I've tried was:

compile: function (uiSwiperConfig, $document, $timeout) {
var me = this;
me.$document = $document;
return {
preLink: function (scope, element, attrs, ngModel) {
me.container = element;
me.userDefinedParams = angular.copy(scope.$eval(attrs.uiSwiper));
me.wrapper = element[0].querySelectorAll("."+me.params.wrapperClass);
me.slides = angular.element(element.find("swiper-slide"));
or me.slides = element[0].querySelectorAll(".slide");
or me.slides = me.wrapper.querySelectorAll(".slide");
...
}
}
}


None of them works except giving me an empty object.

Answer

The best way to implement this is to create an AngularJS element by using

angular.forEach(data.slides, function (slide) {
      var img = angular.element('<img src="'+slide.img+'"/>');
      me.wrapper.append(img);
});