Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap carousel TypeError: f[0] is undefined

We are working with Bootstrap Carousel and want to load dynamic slides (a slide for each item in an array). AngularJS is used to generate array and loop through.

But while running, we got a javascript errorTypeError: f[0] is undefined . The array is filled by $http.get

Tried alternate possibilities from https://docs.angularjs.org/ and http://w3schools.com

  • Carousel is working if array is defined within the script without using $http.get
  • $http.get generated array is displayed properly in "tr ng-repeat"

This is the code:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Fresh</title>
<link rel="stylesheet"
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script
    src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>
<body>
    <div ng-app="myApp" ng-controller="myCtrl">

        <div ng-show="names.length">

            <div id="myCarousel" class="carousel slide" data-ride="carousel">
                <ol class="carousel-indicators">
                    <li data-target="#myCarousel" data-slide-to="{{ $index }}"
                        ng-repeat="x in names"></li>
                </ol>

                <div class="carousel-inner" role="listbox">
                    <div class="item" ng-repeat="x in names">{{x.Name}}
                        {{x.Country}}</div>
                </div>

                <a class="left carousel-control" href="#myCarousel" 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="#myCarousel" 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>

    <script>

        // window.alert('Inside script');
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope, $http) {
            // window.alert( 'Inside controller' );
            $scope.names = [];
            $http.get("http://www.w3schools.com/angular/customers.php").then(
                    function(response) {
                        $scope.names = response.data.records;
                        window.alert('Inside get');
                    });

        });
    </script>
</body>
</html>
like image 942
Tejesvini Avatar asked Feb 27 '16 15:02

Tejesvini


2 Answers

remove data-ride="carousel" from your div

like image 135
oezix Avatar answered Nov 12 '22 18:11

oezix


In the carousel-inner there should be one item which is also active

I don't know how to do this in angular, but this is how i did it using php. look at the line below the comment.

<div class="carousel-inner" role="listbox">
<?php foreach ($pages as $i => $page): ?>

    // make first item active
    <div class="item <?php if($i == 0) { echo "active"; } ?>">
    <img src="data:image/jpg;base64,<?php echo $page['data'] ?>" alt="...">
        <div class="carousel-caption">
            <?php echo "asdfsadfasd" ?>      
        </div>
    </div>
<?php endforeach; ?>
</div>
like image 45
Kinjal Dixit Avatar answered Nov 12 '22 17:11

Kinjal Dixit