Flexslider not working properly when using ng-repeat
Asked Answered
O

2

6

I am having issues with flex slider as it stops working if I use ng-repeat. Otherwise its working fine.

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>

I have recreated this issue in a plunker http://plnkr.co/edit/P2AOwQY0fQSMSXUQbc9t?p=preview

Overwinter answered 20/4, 2016 at 13:36 Comment(0)
S
1

You have to delay the flexslider until the everything inside your directive is rendered. You can use the $timeout service:

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

See plnkr.

Snead answered 20/4, 2016 at 18:5 Comment(2)
there is a small issue with this. It wont show up until I refresh the page..Any idea on what might be the cause?Overwinter
This solution works. I found the issue is to be in my app code itself. The issue was that the carousal is not waiting for the http request to finish. If I cache the result then it works. Thank you for the help.Overwinter
L
2

For some reason the directive didn't work for me, so after a long trial and error period, I came up with the following:

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

and simply call this after you've loaded everything (my images come from a url that's loaded at runtime):

setTimeout( startSlideShow, 10)
Loiseloiter answered 15/12, 2016 at 3:59 Comment(0)
S
1

You have to delay the flexslider until the everything inside your directive is rendered. You can use the $timeout service:

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

See plnkr.

Snead answered 20/4, 2016 at 18:5 Comment(2)
there is a small issue with this. It wont show up until I refresh the page..Any idea on what might be the cause?Overwinter
This solution works. I found the issue is to be in my app code itself. The issue was that the carousal is not waiting for the http request to finish. If I cache the result then it works. Thank you for the help.Overwinter

© 2022 - 2024 — McMap. All rights reserved.