[英]Get Data from Dynamically Created textboxes in Angular using ng-for
[英]Adding integers dynamically as user inputs in dynamically created textboxes - Angular JS
我刚刚开始学习Angular JS,并想创建一个简单的计算器。 首先要求用户输入操作数。 此后,我想显示用于输入操作数的大量文本框。 以下是html(这不是计算器的完整代码):
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" ng-app="myCalc">
<head>
<meta charset="utf-8" />
<title>Calculator</title>
</head>
<body ng-controller="FormController">
<h3><em>Do your calculations here!!</em></h3>
<div>
<p>Enter number of operands : <input type="number" ng-model="paramCount" name="count" ng-change="getNumber(paramCount)"/></p>
<p ng-repeat="i in getNumber(paramCount)">Enter the operand : <input type="text" ng-model="numbers"/></p>
<p><button ng-click="reset()">Reset</button></p>
</div>
</body>
</html>
下面是脚本:
<script src="angular.min.js"></script>
<script>
var myCalcApp = angular.module('myCalc', []);
myCalcApp.controller('FormController', function ($scope) {
$scope.paramCount = 0;
$scope.getNumber = function (num) {
array = [];
for (i = 1; i <= num; i++) {
array.push(i)
}
return array;
}
$scope.reset = function () {
$scope.paramCount = 0;
};
});
</script>
我坚持将用户输入绑定到任何模型,并使用它来计算总和。
任何帮助深表感谢。
谢谢。
一种解决方案是:
1)在控制器中创建一个操作数数组。
示例:$ scope.operands = [];
2)在“ $ scope.getNumber”函数中对其进行初始化
3)更改此ng模型:
<input type="text" ng-model="numbers"/>
通过这个:
<input type="text" ng-model="operands[$index]"/>
希望我的回答适合您。
一些有用的文档: https : //docs.angularjs.org/api/ng/directive/ngRepeat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.