“Add More” button for generating dynamic form inpu

2019-08-07 15:02发布

问题:

How can I implement Add More form inputs kind of function using ionic framework and angularjs?

Should be like this -

http://bootsnipp.com/snippets/featured/dynamic-form-fields-add-amp-remove-bs3

回答1:

Just define an array with inputs, and loop through them to display:

$scope.inputs = [
    { value: null }
];

$scope.addInput = function () {
    $scope.inputs.push({ value: null });
}

$scope.removeInput = function (index) {
    $scope.inputs.splice(index, 1);
}

And in your view:

<div ng-repeat="input in inputs">
    <input type="text"
           ng-model="input.value" />
    <button ng-if="$index == inputs.length - 1" 
            ng-click="addInput()">+</button>
    <button ng-if="$index != inputs.length - 1"
            ng-click="removeInput($index)">-</button>
</div>

See this jsfiddle