Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  70] [ 3]  / answers: 1 / hits: 17829  / 9 Years ago, fri, september 25, 2015, 12:00:00

I'm figuring out how can I save the values that are entered in the input text box inside ng-repeat on a single click of a button.I have seen examples like this getting values from text box that lets user to save each text box individually.
Below is a sample code:



$scope.items=[1,2,3,4]

<div ng-repeat=item in items>
<input type=text ng-model=item.id>
</div>
<button type=button ng-click=saveAllValues()>Save</button>

More From » angularjs

 Answers
19

You just bind the ng-model to another object and use $index to refer to the index within the very ng-repeat:





var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
$scope.items = [1, 2, 3, 4];
$scope.values = [];

$scope.saveAllValues = function() {
alert($scope.values);
}
});

<script src=https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js></script>

<div ng-app=myApp ng-controller=MyCtrl>
<div ng-repeat=item in items>
<input type=text ng-model=values[$index]>
</div>
<button type=button ng-click=saveAllValues()>Save</button>
</div>




[#64930] Wednesday, September 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reesel

Total Points: 345
Total Questions: 124
Total Answers: 119

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
2 Years ago
;