parseInt 从单选按钮 AngularJS 返回值

use*_*352 3 javascript radio-button angularjs

这几天我一直在试图解决这个问题......

我有一组单选按钮,用于对 1 到 5 之间的内容进行评分,每个按钮设置为 value = 1、value = 2 等,作为评论表单的一部分。但是,当数据返回到控制器并推送到 db.json 文件时,单选按钮值的类型为字符串。我如何让它们成为 int 类型?

单选按钮 HTML 代码:

<label class = "radio-inline" for = "one"><input type = "radio"
name = "rating" id = "one" data-ng-model = "userComment.rating" 
value = 1 required>1</label>
Run Code Online (Sandbox Code Playgroud)

来自controllers.js的代码:

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

我尝试过这个想法,认为它会起作用,但事实并非如此。

$scope.userComment.rating = parseInt($scope.userComment.rating);
Run Code Online (Sandbox Code Playgroud)

And*_* D. 5

只需尝试使用ng-value而不是value属性:

<label class = "radio-inline" for = "one"><input type = "radio"
name = "rating" id = "one" data-ng-model = "userComment.rating" 
ng-value = "1" required>1</label>
Run Code Online (Sandbox Code Playgroud)