javascript - Angular js:ng-model is over writing the items in the ng-repeat -
i have strange problem ng-repeat array.
currently, listing array using ng-repeat
, edit button placed edit particular item , save database.
but problem when particular item
in text box changed ng-repeat
items changed.
here fiddle of issue
https://plnkr.co/edit/d5nqitsrg7scfzbe9iab?p=preview
change value in textfield affect values in ng-repeat
you assigning reference of variable that's why changing. have copy variable. below working example:-
var app = angular.module("myapp", []); app.controller("myctrl", function($scope) { $scope.items= [ {id:1,name:'first'},{id:2,name:'second'} ]; $scope.edititem = function(index){ //edit $scope.edit = angular.copy($scope.items[index]); } });
<!doctype html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app="myapp" ng-controller="myctrl"> <p ng-repeat="item in items">{{item.id+','+item.name}} <span ng-click="edititem($index)">click edit {{item.name}} item</span> </p> <h2>edit</h2> <input type="text" ng-model="edit.name" /> </div>
Comments
Post a Comment