0

To shorten up the post this is all the info needed for my problem, so i made a button that gives +1 rating and a button for -1 rating when clicked and this is its HTML code,

<p>
<strong>Rating: </strong> {{ album.rating }}
<button class="btn btn-small" ng-click="upVoteRating(album)">+</button>
<button class="btn btn-small" ng-click="downVoteRating(album)">-</button>
</p>

Nothing unusual, heres the angularjs code using scope and the function for the button.

$scope. upVoteRating = 'upVoteRating';
$scope. downVoteRating = 'downVoteRating';

function upVoteRating(album) {

album.rating++;

}

function downVoteRating(album) {

if (album.rating > 0)
album.rating--;

}

So theres no problem with the code by my guessings but everytime i click the button on my page, it says this on the console:

"TypeError: string is not a function
at http://localhost:1234/lib/angular/angular.min.js:168:37
at http://localhost:1234/lib/angular/angular.min.js:186:167
at g.$eval (http://localhost:1234/lib/angular/angular.min.js:104:308)
at g.$apply (http://localhost:1234/lib/angular/angular.min.js:105:48)
at HTMLButtonElement.o.event.dispatch (http://localhost:1234/lib/jquery-2.1.0.min.js:3:6055)
at HTMLButtonElement.r.handle (http://localhost:1234/lib/jquery-2.1.0.min.js:3:2830) angular.js:9507"

I've got no idea how to fix this, i've been looking for an hour, so please, if you need more code to help you out to find the problem out ask right away!

Thanks again!

1
  • The problem is exactly what the debugger is telling you. A string is not a function. You can't put a string where a function is expected. Try $scope. upVoteRating = upVoteRating; - JavaScript functions are first order, you can pass them like that. Commented Apr 5, 2014 at 22:35

1 Answer 1

3

You're setting it as a string here:

$scope.upVoteRating = 'upVoteRating';

and trying to call it as a function here

<button class="btn btn-small" ng-click="upVoteRating(album)">+</button>

Remove the quotes in the controller and you should be good:

$scope.upVoteRating = upVoteRating;
$scope.downVoteRating = downVoteRating;
Sign up to request clarification or add additional context in comments.

3 Comments

This sounds correct, moreover, you can skip naming the function altogether and simply write $scope.upVoteRating = function(){...
Thanks so much, i guess i should pay more attentiion!
@user3502346 Think of it as you now know how to solve this problem (and prevent it) in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.