AngularJS: Iterating over object properties and two way binding -
i want loop on object's property. also, there should two-way data binding change change object well.
both key , value of object property can changed ui , should reflect in object's structure.
i able object's value ng-model="contents[index]"
but how object property key e.g. interface in object change if change on ui.
$scope.contents = { "interface": "gigabitethernet", "interfacenumber": "1", "bgpneighborip": "00.0.112.499", "bgpremoteas_[1]": "701", "bgpneighborip_[2]": "8.3.112.170", "bgpremoteas_[2]": "702" }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <tbody> <tr ng-repeat="(index, p1) in contents"> <td> <input class="form-control" type="text" ng-model="index"> </td> <td> <input class="form-control" type="text" ng-model="contents[index]"> </td> </tr> </tbody>
try solution:
angular.module('app',[]).controller('test',['$scope', function($scope){ $scope.contents = { "interface": "gigabitethernet", "interfacenumber": "1", "bgpneighborip": "00.0.112.499", "bgpremoteas_[1]": "701", "bgpneighborip_[2]": "8.3.112.170", "bgpremoteas_[2]": "702" }; $scope.arr = []; for(var prop in $scope.contents) $scope.arr.push({oldkey:prop, newkey:prop}); $scope.change = function(item){ $scope.contents[item.newkey] = $scope.contents[item.oldkey]; delete $scope.contents[item.oldkey]; item.oldkey = item.newkey; } }])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='app' ng-controller='test'> <input ng-repeat-start='item in arr' type='text' ng-model="item.newkey" ng-change='change(item)'> <input type='text' ng-model="contents[item.oldkey]" > <br ng-repeat-end> <p>{{contents|json}}</p> </div>
Comments
Post a Comment