简体   繁体   English

Angular- 从中获取数据绑定值

[英]Angular- Get data-bind value from <span>

I'm having a complete table as a form, most of the cells in the table(td) are input fields我有一个完整的表格作为表格,表格(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>

when i try to get $scope.addResources.total after submitting , it's giving null value, and i had to use angular.element() to get it's value.当我在提交后尝试获取 $scope.addResources.total 时,它给出了空值,我不得不使用 angular.element() 来获取它的值。 What might be the issue here这里可能有什么问题

Edit 1:编辑1: 在此处输入图片说明

those who are wondering why, have a look at first 4 columns, they are user input rest are all with calculation, as user enters/changes value the last 5 columns will updates by itself.那些想知道为什么的人,看看前 4 列,它们是用户输入的其余部分都在计算,当用户输入/更改值时,最后 5 列将自行更新。 If i need to have row wise calculation (total or percentage) now i need those ng-model value如果我现在需要按行计算(总数或百分比),我需要那些 ng-model 值

First ng-model directive should be used within an input (textarea, select...) element, as the documentation says.正如文档所说,第一个 ng-model 指令应该在输入 (textarea, select...) 元素中使用。 [ https://docs.angularjs.org/api/ng/directive/ngModel] [ https://docs.angularjs.org/api/ng/directive/ngModel ]

As addResources.total as addResources.totalsecond are calculated fields you could have them calculated on your controller, there is no reason to have ng-model inside the span.由于 addResources.total 和 addResources.totalsecond 是计算字段,您可以在控制器上计算它们,因此没有理由在跨度内使用 ng-model。

try this.尝试这个。 aslo you can compute it in 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM