Bootstrap carousel not working with angularjs

后端 未结 9 604
执笔经年
执笔经年 2020-12-03 04:55

I want to use bootstrap carousel along with angular js to display images in carousel content. But, when the navigation links clicked, it displays blank page.

My code

相关标签:
9条回答
  • 2020-12-03 05:07

    If you don't need to bind your carousel, you can solve this by adding a top level element with ng-non-bindable directive:

    <div ng-non-bindable>
        <div id="Carousel" class="carousel slide">
            <!-- carousel content -->
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-03 05:07

    Your code works fine.

    No any problem in the code you showed.

    http://plnkr.co/edit/1vq7DUXegQdBq3jvj7Zy?p=preview

    0 讨论(0)
  • 2020-12-03 05:11

    As mentioned, the issue is with AngularJS and ngRoute intercepting the a-link clicks. I had this problem but did not want to use Angular UI Bootstrap (as described in other answers).

    So, I changed the .carousel-control elements from links (a tags) to spans.

    <span class="left carousel-control" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </span>
    <span class="right carousel-control" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
    </span>
    

    Then, I added an event handler as follows...

      $('.carousel').carousel({
                    interval: 5000,
                    pause: "hover",
                    wrap: true
                })
                .on('click', '.carousel-control', handle_nav);
    
      var handle_nav = function(e) {
            e.preventDefault();
            var nav = $(this);
            nav.parents('.carousel').carousel(nav.data('slide'));
      }
    

    Hope this helps...

    0 讨论(0)
  • 2020-12-03 05:13

    This works, without modify the original bootstrap example

    $("a.carousel-control").click(function(e){
        e.preventDefault();
        $(this).parent().carousel($(this).data("slide"));
    });
    
    0 讨论(0)
  • 2020-12-03 05:16

    Set target="_self" in your carousel controls:

    <a class="left carousel-control" href="#Carousel" data-slide="prev" target="_self">
    
    0 讨论(0)
  • 2020-12-03 05:17

    You can make function and place it on prev/next arrow in html.

    $scope.prevSlide = function() {
        $('#MY-CAROUSEL-ID').carousel('prev');
    };
    
    0 讨论(0)
提交回复
热议问题