我正在使用ng表單和ng-repeat動態表單插入並嘗試使用該表單中的文本框存儲多個動態值。 這裏是我的代碼:帶有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>
對這個網站的角度控制器代碼如下:
$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: ''
})
任何形式的幫助是值得歡迎的,因爲這將有助於我和其他的程序員太...
如果我在第一個表單文本框中添加一個值,這個值會在其他動態表單中反映出來......我需要解決方案來擺脫此問題...我已經實現了單個表單的文本框值的動態插入但是當它涉及多個表單動態像上面那麼我卡住這個問題... – sunilsmith
你可以從它得到一些想法http://plnkr.co/edit/F7BGq0sfSLvKE48eDTzQ?p=preview,而不是確切的解決方案,考慮爲準則。 – Jigar7521
https://plnkr.co/edit/MvaJlS0nOb2ukWyn6DcW?p=preview我已將我的問題的副本在plunker請看看它@JigarPrajapati – sunilsmith