Adeel Khan Adeel Khan - 12 days ago 4
AngularJS Question

how to add new row data in current json array angularjs

i have added new row but i want update new row data also in database but there is no updated data how to get new row data also add address json array

<tr ng-repeat="x in Profile.addresses">
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td>





<tr ng-repeat="lines in array">

<td><input type="text" class="form-control" id="inputDefault" ng-model='x.site_name ' name='site_name'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.street_address ' name='street_address'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.city ' name='city'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.state ' name='state'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.country ' name='country'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.zip_code ' name='zip_code'></td>
<td><input type="text" class="form-control" id="inputDefault" ng-model='x.phone_number ' name='phone_number'></td>

</tr>




<div class="col-md-2">

<a href="" data-toggle="tooltip" title="Add Address" ng-click="addRow()"><i class="fa fa-plus fa-2x cust_primary" aria-hidden="true">





enter image description here

Answer

In your controller, create a counter and an array.

$scope.i = 0;
$scope.array = [];

Everytime your user clicks on the button, add one to the counter and create the array.

$scope.addRow = function() {
    $scope.i++;
    $scope.array = [];
    for(var i = 0; i < $scope.i; i++) {
        $scope.array.push(i);
    }
}

In your html, simply repeat the lines based on that counter.

<tr ng-repeat="lines in array">
    // Your tds
</tr>
Comments