0

indexCtrl.js

var app = angular.module('app', ['ui.router', 'ui.bootstrap', 'ngAnimate'])
.config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
  $scope.myInterval = 5000;
  $scope.noWrapSlides = false;
  $scope.active = 0;
  var slides = $scope.slides = [];
  var currIndex = 0;



  $scope.addSlide = function() {

  $http.get('/find').success(function(data) {

    for (var i = 0; i < data.length; i++) {
        var discovery = {};
        discovery.image = data[i].image;
        discovery.name = data[i].name;
        discovery.objectType = data[i].objectType;
        discovery.description = data[i].description;
        discovery.discoveredBy = data[i].user;
        discovery.discoveredOn = data[i].discoveredOn;
        discovery.location = data[i].location;

        $scope.discoveries.push(discovery);

    } // end of for loop

    for (var i = 0, i < 4, i++) {
        var discovery = {};
        var randNum = Math.floor(Math.random() * discoveries.length);
        slides.push({
            image = discoveries[randNum].image;
            name = discoveries[randNum].name;
            objectType = discoveries[randNum].objectType;
            description = discoveries[randNum].description;
            discoveredBy = discoveries[randNum].user;
            discoveredOn = discoveries[randNum].discoveredOn;
            location = discoveries[randNum].location;
        });
    }

});



// var newWidth = 600 + slides.length + 1;
// slides.push({
//   image: '//unsplash.it/' + newWidth + '/300',
//   // image: '../../images/jumbotron.jpg',
//   text: ['Nice image','Awesome photograph','That is so cool','I love that'][slides.length % 4],
//   id: currIndex++
// });
};

$scope.randomize = function() {
var indexes = generateIndexesArray();
assignNewIndexesToSlides(indexes);
};

for (var i = 0; i < 4; i++) {
  $scope.addSlide();
}

// Randomize logic below

function assignNewIndexesToSlides(indexes) {
  for (var i = 0, l = slides.length; i < l; i++) {
    slides[i].id = indexes.pop();
  }
}

function generateIndexesArray() {
  var indexes = [];
  for (var i = 0; i < currIndex; ++i) {
    indexes[i] = i;
  }
  return shuffle(indexes);
}

// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;

if (top) {
  while (--top) {
    current = Math.floor(Math.random() * (top + 1));
    tmp = array[current];
    array[current] = array[top];
    array[top] = tmp;
  }
}

  return array;
}




}]);

I am trying to grab information from my database and use four random objects in the demo carousel instead of what angular ui bootstrap has for their demo. I am currently getting this error: "Argument 'CarouselDemoCtrl' is not a function, got undefined" when I use my current $scope.addSlide function. However, it does not throw any errors and works fine when I have the commented out section in $scope.addSlide function instead. Why would this effect my CarouselfDemoCtrl in a way to make it not a function and how might I go about fixing it? Again, the carousel was working until I changed the $scope.addSlide function.

This is my app.js:

var app = angular.module('app', ['ui.router', 'ui.bootstrap', 'ngAnimate'])
.config(['$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {

    $urlRouterProvider.otherwise('/');

    $stateProvider
    .state('profile', {
        url: '/profile',
        templateUrl: 'views/profile.html',
        controller: 'profileCtrl'
    })
    .state('discover', {
        url: '/discover',
        templateUrl: 'views/discover.html',
        controller: 'discoverCtrl'
    })
    .state('find', {
        url: '/find',
        templateUrl: 'views/find.html',
        controller: 'findCtrl'
    })
    .state('index', {
        url: '/index',
        templateUrl: 'views/index.html',
        controller: 'CarouselDemoCtrl'
    })

}]);

This is the index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Discover It!</title>
<script src = "https://plus.google.com/js/client:platform.js" async defer></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>

<body>




<div class="container" ng-app="app">
<div ng-controller="CarouselDemoCtrl">
  <div style="height: 305px">
    <div uib-carousel active="active" interval="myInterval" no-wrap="noWrapSlides">
      <div uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id">
        <img ng-src="{{slide.image}}" style="margin:auto;">
        <div class="carousel-caption">
          <h4>Name: {{slide.name}}</h4>
          <p>{{slide.description}}</p>
        </div>
      </div>
    </div>
  </div>
</div>
</div>


<script src="https://code.jquery.com/jquery-2.2.3.js" integrity="sha256-laXWtGydpwqJ8JA+X9x2miwmaiKhn8tVmOVEigRNtP4=" crossorigin="anonymous"></script>
<script src="//widget.cloudinary.com/global/all.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.js"></script>
<script src="scripts/angular-animate/angular-animate.min.js"></script>
<script src="scripts/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js"></script>

<script src="scripts/app.js"></script>
<script src="scripts/controllers/profileCtrl.js"></script>
<script src="scripts/controllers/discoverCtrl.js"></script>
<script src="scripts/controllers/findCtrl.js"></script>
<script src="scripts/controllers/indexCtrl.js"></script>

I have updated to add where my code currently is and more information since maybe my issue is coming from a different area.

Robert Prine
  • 2,633
  • 5
  • 14
  • 17
  • Do you have any more files in which you are including //angular.module(app,[]) ? – Pritish Vaidya Sep 05 '16 at 03:50
  • Yes, three others without [] and one other with []. I have tried taking out the brackets in my current controller (CarouselDemoCtrl), but that gives me the same error still. – Robert Prine Sep 05 '16 at 03:52
  • Yes,it might override the module you previously created. var app=angular.module('myApp',[]); basically creates a new module/app whereas var app=angular.module('myApp'); is used to access it. – Pritish Vaidya Sep 05 '16 at 03:55
  • Sorry, I missed the brackets in your first comment and edited my answer, @pritishvaidya . – Robert Prine Sep 05 '16 at 03:57
  • Try using var app=angular .module('myApp',[]); on the first script being loaded and var app=angular.module('myApp'); on the other scripts accessing it. – Pritish Vaidya Sep 05 '16 at 04:00
  • I'm getting this error now: Module 'myapp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. I changed to what you suggested and modified my view to include myApp as well. I've checked spelling and upper lower case on them 5 times. – Robert Prine Sep 05 '16 at 04:12
  • Sorry i didn't mean to include *myApp.Its just the same as the previous *app inside the application.Use the same name as you were using before. – Pritish Vaidya Sep 05 '16 at 04:15
  • I changed both myApps to app and I got the original problem back. Maybe my issue is coming from a different problem in my code that I left out. I included everywhere that references CarouselDemoCtrl in the question above. Thank you so much for your help so far, @pritishvaidya ! – Robert Prine Sep 05 '16 at 12:38

1 Answers1

0

The problem was that I had errors in my for loop logic trying to access information that was undefined, which somehow caused the "Argument 'CarouselDemoCtrl' is not a function, got undefined" error." So if you get an error like this complaining about the controller, try to check your logic too.

Robert Prine
  • 2,633
  • 5
  • 14
  • 17