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

Flexslider not working properly when using ng-repeat

我在使用 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

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

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

参见plnkr

出于某种原因,该指令对我不起作用,所以经过长时间的反复试验,我得出了以下结论:

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();
    })
}

并在加载所有内容后简单地调用它(我的图像来自运行时加载的 url):

setTimeout( startSlideShow, 10)