Angular JS - 基于单选按钮选择的动态值总和

时间:2017-01-29 15:22:36

标签: angularjs

在视图中有两个单选按钮列表

L1

<input type="radio" name="SET1" ng-model="SET1Selected" value="1"> 1
<input type="radio" name="SET1" ng-model="SET1Selected" value="2"> 2
<input type="radio" name="SET1" ng-model="SET1Selected" value="3"> 3

L2

<input type="radio" name="SET2" ng-model="SET2Selected" values="4"> 4
<input type="radio" name="SET2" ng-model="SET2Selected" value="5"> 5
<input type="radio" name="SET2" ng-model="SET2Selected" value="6"> 6

<div> {{value of radio button selected from L1}}+{{value of radio button selected from L2}}</div>

总和将根据L1和L2的选择而改变

2 个答案:

答案 0 :(得分:1)

您可以使用表达式

<div> {{parseInt(SET1Selected)+parseInt(SET2Selected) }}</div>

<强>样本

&#13;
&#13;
var app = angular.module("myApp", []);
 app.controller("myCtrl", ['$scope', function($scope) {
$scope.parseInt = parseInt;

 }]);
&#13;
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <meta charset="utf-8" />
  <script src="https://code.angularjs.org/1.4.7/angular.js"></script>
  <script src="app.js"></script>
</head>

<body ng-controller="myCtrl">
  <div class="row">
  <input type="radio" name="SET1" value="1" ng-model="SET1Selected" > 1
  <input type="radio" name="SET1" value="2" ng-model="SET1Selected"  > 2
  <input type="radio" name="SET1" value="3" ng-model="SET1Selected" > 3
  </div>
  <div class="row">
  <input type="radio" name="SET2" value="4" ng-model="SET2Selected" > 4
  <input type="radio" name="SET2" value="5" ng-model="SET2Selected" > 5
  <input type="radio" name="SET2" value="6" ng-model="SET2Selected" > 6
  </div>
  <div class="row">
  <div> {{parseInt(SET1Selected)+parseInt(SET2Selected) }}</div>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下小提示可以使用

{{(SET1Selected - 0) + (SET2Selected - 0)}}

或者

{{(SET1Selected*1) + (SET2Selected*1)}}

演示:

https://codeide.co/wCudpQka