Angularjs 获取字段数
Angularjs get number of fields
我想问你想要多少个选择? 如果用户输入 10,则应显示 10 个选项。我努力尝试,但这会产生内存错误。
动态表单.jsp
<!DOCTYPE html>
<html ng-app="dynamicFieldsPlugin">
<head>
<!-- <link data-require="bootstrap@3.3.2" data-semver="3.3.2" rel="stylesheet" href="themes/bootstrap.min.css" />
<script data-require="bootstrap@3.3.2" data-semver="3.3.2" src="js/bootstrap.min.js"></script>
<script data-require="jquery@2.1.3" data-semver="2.1.3" src="js/jquery-2.1.3.min.js"></script>
-->
动态表单字段创建插件
<label for="choice" ng-show="showChoiceLabel(choice)">Choices</label>
<label>How many Choices you want?</label>
<input type="text" ng-modal="{{nochoices}}" name="" value="{{nochoices}}">
<button ng-show="showAddChoice(choice)" ng-click="addNewChoiceno(nochoices)">Add Choice</button>
<div class="form-group" data-ng-repeat="choice in choices">
<br/><br/>
<!-- <button ng-show="showAddChoice(choice)" ng-click="addNewChoice()">Add Choice</button> -->
<button ng-click="removeNewChoice()">Remove Choice</button>
<input type="text" ng-modal="{{choice.name}}" name="" placeholder="Enter a restaurant name" value="{{choice.id}}">
</div>
</div>
<!--jQuery Scripts -->
<script src="js/angularjs/1.4.8/app.js"></script>
</body>
应用.js
var app = angular.module("dynamicFieldsPlugin", []);
app.controller("dynamicFields", function($scope) {
$scope.nochoices = 10;
$scope.choices = [{id: 'choice1', name: 'choice1'}];
$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id' : 'choice' + newItemNo, 'name' : 'choice' + newItemNo});
};
$scope.addNewChoiceno = function(nochoices) {
for(var i=$scope.choices.length;i<nochoices+$scope.choices.length;i++){
$scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
}
$scope.choices.length=$scope.choices.length+nochoices;
};
$scope.removeNewChoice = function() {
var newItemNo = $scope.choices.length-1;
if ( newItemNo !== 0 ) {
$scope.choices.pop();
}
};
$scope.showAddChoice = function(choice) {
return choice.id === $scope.choices[$scope.choices.length-1].id;
};
});
错误
内存不足
在这里,看起来 for 循环将继续,而i
小于 $scope.choices.length + nochoices
for(var i=$scope.choices.length;i<nochoices+$scope.choices.length;i++){
$scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
}
不幸的是,您正在循环中推动$scope.choices
。这意味着如果i
增长 1,则$scope.choices.length
也意味着循环条件将始终为真,并且循环将永远持续下去。
也许将上面的代码更改为:
for(var i=$scope.choices.length;i<nochoices;i++){
$scope.choices.push({'id' : 'choice' + i, 'name' : 'choice' + i});
}
相关文章:
- 添加和删除隐藏字段数组中的值,而不提交表单
- 我如何让我的 var 错误消息输出是使用字段还是字段,具体取决于留空的字段数
- 如何在字段数组中更新
- 更新其中一个字段时刷新其他输入字段.使用Angularjs(附Plunker)
- Angularjs 获取字段数
- 在MongoDB中,从文档中获取单个字段数组的最简单方法是什么
- 具有相同名称和不同 id 的选择字段数组
- 流星自动体形 - 禁用对象字段数组中的选择选项
- 克隆的输入字段数组的唯一 ID 值
- 如何根据输入字段数组验证禁用提交按钮
- 自动将数据插入到字段中angularjs
- 为什么我的日期输入字段在AngularJS中抛出类型错误
- 如何使用计数器添加动态字段以限制jQuery中的字段数
- HTML加载用户单击按钮时输入的输入字段数
- 如何通过输入字段数组编辑成分
- 如何具有带名称的字段数组
- 根据jquery中填写的字段数计算并自动更新百分比
- 如何从json字段数组动态创建一个没有jquery的表单
- 根据字段数添加具有自定义属性的字段
- AngularJS过滤记录中对应字段(数组)的值