AngularJS指令如何获得更深层的树节点

时间:2016-08-15 15:52:02

标签: javascript angularjs jquery-ui angularjs-directive swiper

我的html视图如下所示:

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

我的问题是如何在指令中获取幻灯片对象? 我试过的是:

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

除了给我一个空物体外,它们都不起作用。

1 个答案:

答案 0 :(得分:0)

实现此目的的最佳方法是使用

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