John M. John M. - 3 months ago 8
AngularJS Question

Triple checked code and can't find error

I'm following along in a tutorial from a book of mine and my HTML editor keeps telling me that there is an error with one of my lines of javascript. I've checked it item by item at least three different times and cannot spot any deviation from the book. I also went to the books errata page and there was nothing listed there. Can someone please have a look at this and tell me where the problem lies. The editor highlights the first line of my controller area as being my problem child. Thank you.

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})

.controller('MainCtrl', [$scope', function($scope) {
$scope.ratingArr = [{
value: 1,
icon: 'ion-ios-star-outline'
}, {
value: 2,
icon: 'ion-ios-star-outline'
}, {
value: 3,
icon: 'ion-ios-star-outline'
}, {
value: 4,
icon: 'ion-ios-star-outline'
}, {
value: 5,
icon: 'ion-ios-star-outline'
}];


$scope.setRating = function(val) {
var rtgs = $scope.ratingArr;
for (var i = 0; i < rtgs.length; i++ {
if (i < val) {
rtgs[i].icon = 'ion-ios-star';
} else {
rtgs[i].icon = 'ion-ios-star-outline';
}
};
}

}])

Answer

Replace by:

for (var i = 0; i < rtgs.length; i++;) {
    if (i < val) {
        rtgs[i].icon = 'ion-ios-star';
    } else {
        rtgs[i].icon = 'ion-ios-star-outline';
    }
};