Search code examples
angularjssliderangularjs-animationng-switch

using ng-switch and ng-repeat for a slider


I can't make this kind of slider work, taking data from the controller.

This example works fine:

<div class="panel panel-default" ng-controller="SliderCtrl">
    <div class="panel-body"  ng-switch="selectedSlide">
        <img  src="img0.jpg" ng-switch-when="0" class="my-switch-animation" />
        <img  src="img1.jpg" ng-switch-when="1" class="my-switch-animation" />
        <img  src="img2.jpg" ng-switch-when="2" class="my-switch-animation" />
        <img  src="img3.jpg" ng-switch-when="3" class="my-switch-animation" />
        <img  src="img4.jpg" ng-switch-when="4" class="my-switch-animation" />
        <img  src="img5.jpg" ng-switch-when="5" class="my-switch-animation" />
    </div>
</div>

But if I change the img tag for a ng-repeat which should result in the same code, it doesn't work.

<div class="panel panel-default" ng-controller="SliderCtrl">
    <div class="panel-body"  ng-switch="selectedSlide">
        <img  ng-repeat="slide in slides" src="{{ slide.img }}" ng-switch-when="{{ slide.id }}" class="my-switch-animation" />
    </div>
</div>

How can I achieve that?


Solution

  • ng-switch-when doesn't support expression so I'd suggest you to use ng-if work same as like ng-switch-when

    <div class="panel panel-default" ng-controller="SliderCtrl">
        <div class="panel-body">
            <img  ng-repeat="slide in slides" src="{{ slide.img }}" ng-if="slide.id == selectedSlide" class="my-switch-animation" />
        </div>
    </div>
    

    Related Plunkr Without images