我对AngularJS有疑问。在我的表格中,我有几个输入。我必须对数据做一些数学运算,然后保存它。但是因为我使用Angularfire,我必须将结果分配给ng模型,这就是我被困住的地方。我怎么能这样做?
这是我的代码:
<label>First</label>
<input name="firstmatch" ng-model="project.firstmatch">
<label>Second</label>
<input name="secondmatch" ng-model="project.secondmatch">
<label>Third</label>
<input name="thirdmatch" ng-model="project.thirdmatch">
<label>Fourth</label>
<input name="fourth" ng-model="project.fourthmatch">
<label>Fifth</label>
<input name="fifthmatch" ng-model="project.fifthmatch">
<!-- The math part-->
<textarea name="points" ng-model="project.points"> {{ project.firstmatch--project.secondmatch--project.thirdmatch--project.fourthmatch--project.fifthmatch }} </textarea>
谢谢!
答案 0 :(得分:1)
为什么要在textarea中打印结果? Textareas默认不接受代码。只需尝试以div或span打印结果。
如果您希望结果可编辑,则将其打印在另一个输入中,不要在其中进行计算。执行控制器中的逻辑,如下所示:
<label>First</label>
<input name="firstmatch" ng-model="project.firstmatch">
<label>Second</label>
<input name="secondmatch" ng-model="project.secondmatch">
<label>Third</label>
<input name="thirdmatch" ng-model="project.thirdmatch">
<label>Fourth</label>
<input name="fourth" ng-model="project.fourthmatch">
<label>Fifth</label>
<input name="fifthmatch" ng-model="project.fifthmatch">
<!-- The math part-->
<input type="text" name="points" ng-model="project.points" />
而不是在控制器中写道:
$scope.project.points = $scope.project.firstmatch - $scope.project.secondmatch - $scope.project.thirdmatch - $scope.project.fourthmatch - $scope.project.fifthmatch;