使用ng-repeat时,Flexslider无法正常工作

时间:2016-04-20 13:36:54

标签: javascript jquery angularjs flexslider

我遇到flex滑块问题,因为如果我使用ng-repeat,它会停止工作。否则它的工作正常。

myApp.controller('frontCtrl', function ($scope) {
  var results = {"id":4,"title":"sddddddd", "photos":[{"url":"http://placekitten.com/g/400/200","id":1},{"url":"http://placekitten.com/g/400/200","id":2}]};
  $scope.images=results.photos

});

myApp.directive('flexslider', function () {

  return {
    link: function (scope, element, attrs) {

      element.flexslider({
        animation: "slide"
      });
    }
  }
});

HTML

    <div class="flexslider" flexslider>
      <ul class="slides">

        /* This wont work*/
        <li ng-repeat="img in images">
          <img src="{{img.url}}">
        </li>


          /* This work*/
        <li>
          <img src="http://placekitten.com/g/400/200">
        </li>
        <li>
          <img src="http://placekitten.com/g/400/200">
        </li>
        <li>
          <img src="http://placekitten.com/g/400/200">
        </li>
      </ul>
    </div>

我已在plunker http://plnkr.co/edit/P2AOwQY0fQSMSXUQbc9t?p=preview

中重新创建了此问题

2 个答案:

答案 0 :(得分:2)

由于某些原因,该指令对我没有用,所以在经过长时间的试错期后,我想出了以下内容:

function startSlideShow() {   
    jQuery('.slideshow').flexslider({
        animation: "slide",
        animationLoop: false,
        itemWidth: 240,
        controlNav: false
    });
    jQuery('#menu-section a.dropdown-toggle').click(function () {
        jQuery('#menu-section .dropdown-menu').toggle();
    })
}

并在您加载完所有内容后调用此内容(我的图片来自于在运行时加载的网址):

setTimeout( startSlideShow, 10)

答案 1 :(得分:1)

您必须延迟flexslider,直到呈现指令内的所有内容。您可以使用$timeout服务:

myApp.directive('flexslider', function ($timeout) {
  return {
    link: function (scope, element, attrs) {
      $timeout(function(){
        element.flexslider({
          animation: "slide"
        });
      })
    }
  }
});

请参阅plnkr