Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parseInt return value from radio button AngularJS

I've been trying to figure this out for a couple of days now...

I've got a set of radio buttons to rate something between 1 and 5 with each set to value = 1, value = 2 and so on as part of a comment form. But when the data is returned to the controller and is pushed to the db.json file, the radio button values are of type string. How do I get them to be of type int?

Radio button HTML code:

<label class = "radio-inline" for = "one"><input type = "radio"
name = "rating" id = "one" data-ng-model = "userComment.rating" 
value = 1 required>1</label>

code from controllers.js:

$scope.submitComment = function() {
  $scope.movie.comments.push($scope.userComment);
  myFactory.getMovies().update({id:$scope.movie.id}, $scope.movie);

I tried this thinking it would work, but it didn't.

$scope.userComment.rating = parseInt($scope.userComment.rating);
like image 402
user2655352 Avatar asked May 15 '26 22:05

user2655352


1 Answers

Simply try to use ng-value instead of value attribute:

<label class = "radio-inline" for = "one"><input type = "radio"
name = "rating" id = "one" data-ng-model = "userComment.rating" 
ng-value = "1" required>1</label>
like image 54
Andrew D. Avatar answered May 18 '26 11:05

Andrew D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!