gpt4 book ai didi

javascript - Angular- 从 获取数据绑定(bind)值

转载 作者:行者123 更新时间:2023-11-27 23:28:49 24 4
gpt4 key购买 nike

我有一个完整的表格形式,表格中的大部分单元格(td)都是输入字段

<td><input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.one"> </td>

<td><input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.two"> </td>

<td><input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.three"> </td>


<td><input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.four"> </td>


<td><span id="testingInput2" ng-model="addResources.total">{{addResources.one+addResources.three}}</span> </td>
<td><span id="testingInput2" ng-model="addResources.totalsecond">{{addResources.two+addResources.four}}</span> </td>

当我在提交后尝试获取 $scope.addResources.total 时,它给出了空值,我不得不使用 angular.element() 来获取它的值。这里可能是什么问题

编辑 1: enter image description here

那些想知道为什么的人,看看前 4 列,它们是用户输入,剩下的都是计算,当用户输入/更改值时,最后 5 列将自行更新。如果我需要按行计算(总数或百分比),现在我需要那些 ng-model 值

最佳答案

试试这个。你也可以在 Controller 中计算它。

var app = angular.module("testApp", []);
app.controller('testCtrl', function($scope){
$scope.addResources = {total:""};


$scope.submit = function(total){
alert(total);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testCtrl">
<table>
<tr>
<td>
<input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.one"> </td>
<td>
<input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.two"> </td>
<td>
<input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.three"> </td>
<td>
<input id="testingInput2" type="number" placeholder="0"step="1" ng-model="addResources.four"> </td>
<td>
<input type="hidden" id="testingInput2" ng-model="addResources.total" ng-value="addResources.total = addResources.one + addResources.three ">{{addResources.one+addResources.three}}
</td>
</tr>
{{addResources.total}}

</table>
<input type="button" value="submit" ng-click="submit(addResources.total)">

</div>

关于javascript - Angular- 从 <span> 获取数据绑定(bind)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com