- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ng-form 和 ng-repeat 进行动态表单插入,并尝试使用该表单中的文本框存储多个动态值。这是我的代码:
Want to add Ingredients : <button ng-click="showIngredientForm = !showIngredientForm" ng-class="{'red' : showIngredientForm}">{[{toggleText}]}</button>
<div ng-show="showIngredientForm">
<div ng-repeat="formToAdd in formsToAdd" >
<ng-form name="productFieldForm" >
<div class="form-group">
<label>Option Type</label>
<select name="ingredientName" ng-model="formToAdd.ingredientId" class="form-control">
<option value="">Select Option Type Name</option>
<option ng-repeat="option in ingredientTypeList" value="{[{option._id}]}">{[{option.ingredientTypeName}]}</option>
</select>
<button ng-click="removeForm(formToAdd)" ng-if="formsToAdd.length > 1">Remove</button></div>
</ng-form>
<div ng-if="formToAdd.ingredientId">
<div ng-repeat="itemToAdd in itemsToAdd" >
<div class="form-group">
<ng-form name="userFieldForm" >
<table>
<td>
<label>Option Name</label>
<input type="text" placeholder="Option Name" name="ingredientName" ng-model="itemToAdd.ingredientName" ng-pattern="/^[a-zA-Z ]{3,25}$/" class="form-control" autocomplete="off" ng-required="true" ng-change="verifyDuplicate()" />
<div class="validation_messages" ng-show="userFieldForm.$submitted || userFieldForm.ingredientName.$touched">
<span ng-show="userFieldForm.ingredientName.$error.required">Option cannot be empty</span>
<span ng-show="!userFieldForm.ingredientName.$error.required && userFieldForm.ingredientName.$error.pattern && userFieldForm.ingredientName.$dirty">Option Name Samples Cookie,Saz Mora</span>
</div>
<div class='validation_messages' ng-if='itemToAdd.isDuplicate'>
<span>Duplicate Not Allowed</span>
</div>
</td>
<td>
<button ng-click="remove(itemToAdd)" ng-if="itemsToAdd.length > 1">Remove</button>
</td>
</table>
</ng-form>
</div>
</div>
<div>
<button class="validation_messages" ng-click="addNew()">Add new</button>
</div>
</div>
</div>
<div>
<button class="validation_messages" ng-click="addForm()">Add new Form</button>
</div>
</div>
该 html 的 Angular Controller 代码如下:
$scope.itemsToAdd = [{
ingredientTypeName: ''
}];
$scope.formsToAdd = [];
$scope.$watch('showIngredientForm', function(){
$scope.toggleText = $scope.showIngredientForm ? 'No' : 'Yes';
if($scope.showIngredientForm == true){
$scope.items = [];
$scope.itemsToAdd = [{
ingredientName: '',
ingredientPrice: ''
}];
}
});
$scope.addForm = function(formToAdd) {
$scope.formsToAdd.push({});
}
$scope.remove = function(itemToAdd)
{
var index = $scope.itemsToAdd.indexOf(itemToAdd);
$scope.itemsToAdd.splice(index, 1);
}
$scope.addNew = function() {
$scope.itemsToAdd.push({
ingredientTypeName: '',
ingredientPrice: ''
})
欢迎任何形式的帮助,因为这也会帮助我和其他程序员......
最佳答案
您在输入类型中使用单个对象作为 ng-model
,而不是使用具有多个键和值的对象,例如 ng-model="itemToAdd.ingredientName[formToAdd.ingredientId]"
这将创建一个与 id 相关的唯一键,并将输入文本值分配给该键,这将避免在下一个表单字段中重复出现相同的值。
添加track by
来跟踪列表中的对象,这将帮助我们在添加新字段时创建新的ng-model
对象键。
<div ng-repeat="formToAdd in formsToAdd track by formToAdd.ingredientId">
<div ng-repeat="itemToAdd in itemsToAdd track by $index">
并且输入字段更改为
<label>Option Name</label>
<input type="text" placeholder="Option Name" name="ingredientName" ng-model="itemToAdd.ingredientName[formToAdd.ingredientId]" ng-pattern="/^[a-zA-Z ]{3,25}$/" class="form-control" autocomplete="off" ng-required="true" ng-change="verifyDuplicate()" />
<div class="validation_messages" ng-show="userFieldForm.$submitted || userFieldForm.ingredientName[formToAdd.ingredientId].$touched">
<span ng-show="userFieldForm.ingredientName[formToAdd.ingredientId].$error.required">Option cannot be empty</span>
<span ng-show="!userFieldForm.ingredientName[formToAdd.ingredientId].$error.required && userFieldForm.ingredientName[formToAdd.ingredientId].$error.pattern && userFieldForm.ingredientName[formToAdd.ingredientId].$dirty">Option Name Samples Cookie,Saz Mora</span>
</div>
下面是 JS 中的简单更改
$scope.itemsToAdd = [{
ingredientName: {},//assign a new object everytime
ingredientPrice: ''
}];
在 HTML 中
<body ng-controller="MainCtrl">
Want to add Ingredients :
<button ng-click="showIngredientForm = !showIngredientForm" ng-class="{'red' : showIngredientForm}">{{toggleText}}</button>
<div ng-show="showIngredientForm">
<div ng-repeat="formToAdd in formsToAdd track by formToAdd.ingredientId">
<ng-form name="productFieldForm">
<div class="form-group">
<label>Option Type</label>
<select name="ingredientName" ng-model="formToAdd.ingredientId" class="form-control">
<option value="">Select Option Type Name</option>
<option ng-repeat="option in ingredientTypeList" value="{{option._id}}">{{option.ingredientTypeName}}</option>
</select>
<button ng-click="removeForm(formToAdd)" ng-if="formsToAdd.length > 1">Remove</button>
<!-- <div class="validation_messages">
<span ng-show="validCityingredientTypeName">ingredientType Name is required</span>
</div> -->
</div>
</ng-form>
<div ng-if="formToAdd.ingredientId">
<div ng-repeat="itemToAdd in itemsToAdd track by $index">
<div class="form-group">
<ng-form name="userFieldForm">
<table>
<tr>
<td>
<label>Option Name</label>
<input type="text" placeholder="Option Name" name="ingredientName" ng-model="itemToAdd.ingredientName[formToAdd.ingredientId]" ng-pattern="/^[a-zA-Z ]{3,25}$/" class="form-control" autocomplete="off" ng-required="true" ng-change="verifyDuplicate()" />
<div class="validation_messages" ng-show="userFieldForm.$submitted || userFieldForm.ingredientName[formToAdd.ingredientId].$touched">
<span ng-show="userFieldForm.ingredientName[formToAdd.ingredientId].$error.required">Option cannot be empty</span>
<span ng-show="!userFieldForm.ingredientName[formToAdd.ingredientId].$error.required && userFieldForm.ingredientName[formToAdd.ingredientId].$error.pattern && userFieldForm.ingredientName[formToAdd.ingredientId].$dirty">Option Name Samples Cookie,Saz Mora</span>
</div>
<div class='validation_messages' ng-if='itemToAdd.isDuplicate'>
<span>Duplicate Not Allowed</span>
</div>
</td>
<td>
<button ng-click="remove(itemToAdd)" ng-if="itemsToAdd.length > 1">Remove</button>
</td>
</tr>
</table>
</ng-form>
</div>
</div>
<div>
<button class="validation_messages" ng-click="addNew()">Add new</button>
</div>
</div>
</div>
<div>
<button class="validation_messages" ng-click="addForm()">Add new Form</button>
</div>
</div>
</body>
这是更新 code
更新
要在另一种表单中单击“添加新”时删除每个额外的 boxex,请在 JS 中添加以下代码
$scope.addNew = function(key) {
if ($scope.itemsToAdd[key]== null) {
$scope.itemsToAdd[key] = [];
}
$scope.itemsToAdd[key].push({
ingredientName: {},
ingredientPrice: ''
});
};
$scope.remove = function(itemToAdd,key) {
var index = $scope.itemsToAdd[key].indexOf(itemToAdd);
$scope.itemsToAdd[key].splice(index, 1);
};
在 HTML 中将内部 ng-repeat
改为 itemsToAdd
<div ng-repeat="itemToAdd in itemsToAdd[formToAdd.ingredientId] track by $index">
我们基本上不是创建一个列表,而是创建 itemsToAdd
对象 {}
并为其分配一个带有列表对象的键,该列表对象分别为每个表单保存新项目。
关于javascript - 使用 ng-repeat 动态插入多个表单,每个表单都有动态文本框来获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40131303/
这个问题在这里已经有了答案: final keyword in method parameters [duplicate] (9 个回答) 关闭 8 年前。 在此示例中,声明 Object fina
我的目标:是通过我的函数更新字段获取选定值并使用函数输出值运行它。 问题:当我从列表中选择值时,它不会触发函数,也不会更新字段。 感谢您的帮助。 HTML 12 14 16 18 20 22 24
我有一本具有这种形式的字典: myDict = {'foo': bar, 'foobar baz': qux} 现在,我想拆分字典键中的空格,使其成为下一个键并获取值(重复)。 myDictRev1
vector a; vector b; int temp_holder; cout > temp_holder) a.push_back(temp_holder); cout > temp_h
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我
我正在使用 jquery ui 日期选择器来获取 fromDate 和 toDate 以下是from日期的代码 $("#from_date").datepicker({
我是一名优秀的程序员,十分优秀!