我有ng-repeat
生成的<Select>
集,其中包含一个数值。我希望只要用户更改了<Select>
,就会显示他们的整体数字和。 {{option.score}}
表示我希望分配给总数的数值。
图片:
https://cdn.pbrd.co/images/NGg0gJn.png
模板:
<label class="item item-input item-select" ng-repeat="criteria in tool.criterias">
<div class="input-label">
{{criteria.desc}}
</div>
<select>
<option ng-repeat="option in criteria.options">
{{option.score}} — {{option.answer}}
</option>
</select>
</label>
...
<p>Total: the sum of all the option.score's</p>
控制器:
.controller('FullToolsCtrl', function ($scope, $stateParams, Tools) {
$scope.tool = Tools.get($stateParams.toolId);
});
数据:
{
"criterias": [
{
"desc": "Complexion",
"options": [
{
"answer": "Blue or Pale all over",
"score": "0"
},
{
"answer": "Blue at extremities",
"score": "1"
},
{
"answer": "No cyanosed body or extremities",
"score": "2"
}
]
},
{
"desc": "Pulse",
"options": [
{
"answer": "Absent",
"score": "0"
},
{
"answer": "< 100",
"score": "1"
},
{
"answer": "> 100",
"score": "2"
}
]
},
{
"desc": "Grimace",
"options": [
{
"answer": "No response to stimulation",
"score": "0"
},
{
"answer": "Grimace on suction or aggressive stimulation",
"score": "1"
},
{
"answer": "Cry on stimulation",
"score": "2"
}
]
},
{
"desc": "Activity",
"options": [
{
"answer": "Absent",
"score": "0"
},
{
"answer": "Some flexion",
"score": "1"
},
{
"answer": "Flexed arms and legs resist extension",
"score": "2"
}
]
},
{
"desc": "Respiratory effort",
"options": [
{
"answer": "Absent",
"score": "0"
},
{
"answer": "Weak, irregular, gasping",
"score": "1"
},
{
"answer": "Strong, lusty cry",
"score": "2"
}
]
}
],
"desc": "Scoring tool for neonates.",
"name": "APGAR Score"
}
答案 0 :(得分:0)
没有真正构建到ng-repeat中来处理这个问题,当你希望你的总数出现时,你实际上是在ng-repeat构造之外。通常,您只需将HTML中的表达式绑定到控制器中的函数调用即可。类似的东西:
<p>Total: the sum of all the option.score's {{total)()}}</p>
(其中$ ctrl是你的控制器)。
然后在你的控制器中你有一个映射到它的函数,如:
$ scope.total = function(){ var total = 0 angular.forEach($ scope。criteria.options,function(criteria){ 总计+ = criteria.score } 返回总数 };
如果您使用的是控制器语法,那么它就会略有不同。
答案 1 :(得分:0)
尝试这样的事情:
var app = angular.module("Demo", []);
app.controller("AppController", function($scope) {
$scope.total = 0;
$scope.selectValue = [];
$scope.SumValue = function() {
$scope.total = 0
for (var i = 0; i < $scope.selectValue.length; i++) {
if ($scope.selectValue[i] != undefined)
$scope.total += parseInt($scope.selectValue[i]);
}
}
$scope.data = {
"criterias": [{
"desc": "Complexion",
"options": [{
"answer": "Blue or Pale all over",
"score": "0"
}, {
"answer": "Blue at extremities",
"score": "1"
}, {
"answer": "No cyanosed body or extremities",
"score": "2"
}]
}, {
"desc": "Pulse",
"options": [{
"answer": "Absent",
"score": "0"
}, {
"answer": "< 100",
"score": "1"
}, {
"answer": "> 100",
"score": "2"
}]
}, {
"desc": "Grimace",
"options": [{
"answer": "No response to stimulation",
"score": "0"
}, {
"answer": "Grimace on suction or aggressive stimulation",
"score": "1"
}, {
"answer": "Cry on stimulation",
"score": "2"
}]
}, {
"desc": "Activity",
"options": [{
"answer": "Absent",
"score": "0"
}, {
"answer": "Some flexion",
"score": "1"
}, {
"answer": "Flexed arms and legs resist extension",
"score": "2"
}]
}, {
"desc": "Respiratory effort",
"options": [{
"answer": "Absent",
"score": "0"
}, {
"answer": "Weak, irregular, gasping",
"score": "1"
}, {
"answer": "Strong, lusty cry",
"score": "2"
}]
}],
"desc": "Scoring tool for neonates.",
"name": "APGAR Score"
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Demo">
<div ng-controller="AppController">
<div ng-repeat="criteria in data.criterias">
<label>
{{ criteria.desc }}
</label>
<br/>
<select ng-model="selectValue[$index]" ng-change="SumValue()">
<option ng-repeat="option in criteria.options" value="{{option.score}}">{{option.answer}}</option>
</select>
</div>
<p>Total: the sum of all the option.score's {{ total }}</p>
</div>
</div>
&#13;
JSFiddle:https://jsfiddle.net/d42jeuvs/15/
希望这是你想要的。感谢。