1

I am trying to use a bootstrap carousel in an angular spa application and am having issues getting it to slid automatically. If I put it in a regular html page everything works as expected. I have tried multiple solutions that I have found on stackoverflow (most are still in the code, but commented out), but nothing seems to work. Here are three of my files (app.js, index.html and home.html).

app.js

(function () {
  'use strict';
  window.app = angular.module('app', ['ui.router']);
  app.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/');

  $stateProvider
      .state('home', {
        url: '/',
        templateUrl:'/views/stackoverflow/home.html',
        onEnter: function() {
          $('.carousel').carousel({interval: 300, pause:false});
          //$('.carousel').carousel('cycle');
          //$('.carousel').carousel().next();
          //$('.carousel-control.right').trigger('click');
        }
      })
  });

  app.run(function($rootScope, $location) {
    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
        console.log("toState",toState);
        if ( toState.templateUrl === '/views/stackoverflow/home.html' ) {
            $('.carousel').carousel({interval: 300, pause:false});
            $('.carousel').carousel('cycle');
            //$('.carousel').carousel().next();
            //$('.right.carousel-control').trigger('click');
            //$('.right.carousel-control').click();
        }
    });
  })
}());

Index.html

<!DOCTYPE html>
<html ng-app="app">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>My App</title>
    <link href="http://netdna.bootstrapcdn.com/bootswatch/3.2.0/cerulean/bootstrap.min.css" rel="stylesheet">
    <link href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/content/bootstrap/css/bootstrap.css" />
    <script src="/content/jquery/jquery.js"></script>
    <style>
        [ng-cloak] {display:none !important;}
    </style>
  </head>
  <body>
    <div ui-view ng-cloak></div>
  </body>
  <script src="/content/bootstrap/js/bootstrap.js"></script>
  <script src="/content/angular/angular.js"></script>
  <script src="/content/angular-ui-route/angular-ui-router.js"></script>
  <script src="/app/app.js"></script>
</html>

Main.html

<nav class="navbar navbar-inverse navbar-static-top"> </nav>
<div class="container-fluid">
  <div class="container-fluid">
    <div id="mainCarousel" class="carousel slide" data-ride="carousel">
      <ol class="carousel-indicators">
        <li data-target="#product-info-carousel" data-slide-to="0" class="active"></li>
        <li data-target="#product-info-carousel" data-slide-to="1"></li>
        <li data-target="#product-info-carousel" data-slide-to="2"></li>
      </ol>
      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <img src="/images/image-1.png" alt="item1">
          <div class="carousel-caption">
            <h3>First Slide Label</h3>
            <p>Praesent commodo cursus magna, vel</p>
          </div>
        </div>
        <div class="item">
          <img src="/images/image-2.png" atl="item2">
          <div class="carousel-caption">
            <h3>Second Slide Label</h3>
            <p>Praesent commodo cursus magna, vel</p>
          </div>
        </div>
        <div class="item">
          <img src="/images/image-3.png" atl="item3">
          <div class="carousel-caption">
            <h3>Third Slide Label</h3>
            <p>Praesent commodo cursus magna, vel</p>
          </div>
        </div>
      </div>
      <a class="left carousel-control" href="#mainCarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="right carousel-control" href="#mainCarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>
  </div>
  <div class="container">
    <div class="navbar navbar-inverse navbar-fixed-bottom">
      <div class="navbar-inner navbar-content-center">
        <p>
        </p>
      </div>
    </div>
  </div>
</div>
amphetamachine
  • 27,620
  • 12
  • 60
  • 72
Richard O
  • 139
  • 1
  • 9
  • There is a similar question [here](http://stackoverflow.com/questions/21907843/bootstrap-carousel-not-working-with-angularjs) Also, a side note, you duplicate the `
    ` tag. Not really necessary.
    – CodeBob Feb 20 '15 at 17:45
  • I tried the different options suggested in the list but it still does not seem to work correctly. The page loads with the first item showing but never moves to the second or third. If I click the right or left arrow the carousel will move to the next one and then it begins to work correctly. Waits about 3 seconds and transitions to the next. If I reload the page then it stops working again until I click one of the arrows. – Richard O Feb 21 '15 at 02:24

1 Answers1

0

You need to manually kick off the carousel. So make sure you are hitting the onEnter function(just look for errors in chrome/firefox console)

I would change the function from:

$('.carousel').carousel({interval: 300, pause:false});

to include cycle:

$('.carousel').carousel({interval: 300, cycle:true });

If it still doesn't work you could go another direction and call a

$(document).ready(function() { $('#mainCarousel').carousel({ interval: 300 })});
CodeBob
  • 775
  • 7
  • 14
  • I have tried both approaches and neither one worked. Could it have to do with the use of Angular ui-route? I wonder if that is consuming the event or if the the DOM is not fully loaded when the the method is called. – Richard O Feb 22 '15 at 03:45
  • Yea, I was assuming that the 'ui-view' directive contained the Main.html, is that correct? If so, we know that anything with ng-cloak will be hidden until angular processes, so it may not be finding the carousel when the function is called. Try removing the ng-cloak to test and see if that is the case. – CodeBob Feb 23 '15 at 16:45