Angular JS filter in slider

I need to create a slider (I am actually using flexslider), with data in ng-repeat, but also with filter buttons. I created this code:

Filter

<div class="terms">
            <button ng-click="myFilter = {terms: 'advertising'}">Advertising</button>
        <button ng-click="myFilter = {terms: 'branding'}">Branding</button>
        <button ng-click="myFilter = {terms: 'packaging'}">Packaging</button>
        <button ng-click="myFilter = {terms: 'print'}">Print</button>
        <button ng-click="myFilter = {terms: 'video'}" class="">Video</button>
        <button ng-click="myFilter = {terms: 'web'}" class="active">Web</button>
                <button ng-click="myFilter = {terms: ''}" class="">All</button>
    </div>

      

Slider

 <div class="flexslider" id="project_slider">
            <ul class="slides">
                <li ng-repeat="slide in slides | filter: myFilter">
                    <a href="{{ slide.link }}"><img src="{{ slide.img }}"></a>
                </li>
            </ul>
        </div>

      

Then, after the document is ready for completion, I start the slider:

$(document).ready(function() {
   $('#project_slider').flexslider({
      controlNav: false,
   });
});

      

And the slider works fine, but no filtering. The problem is - I think - with the filter, when I add or remove data using Angular, the slider is not reinitialized. Any suggestions?

+3


source to share


1 answer


You can create a directive and call it flexislider

when rendering the latter ng-repeat

.

Example:



.directive('someDirective', function() {
  return function(scope, element, attrs) {
    if (scope.$last){
      $('#project_slider').flexslider({
         controlNav: false,
      });
    }
  };
})

    <div class="flexslider" id="project_slider">
        <ul class="slides">
            <li ng-repeat="slide in slides | filter: myFilter" some-directive> //<-- added the directive
                <a href="{{ slide.link }}"><img src="{{ slide.img }}"></a>
            </li>
        </ul>
    </div>

      

Extracted from ng-repeat end event

+1


source







All Articles