2

我的ng-repeat数组有一个奇怪的问题。Angular js:ng-model已经结束了在ng-repeat中写入项目

目前,我列出了一个使用ng-repeat的数组,并且放置了一个编辑按钮来编辑特定的项目并保存到数据库中。

但问题是当文本框中的特定item发生更改时ng-repeat项目也发生更改。

这里是我的问题

https://plnkr.co/edit/D5NQitsRg7sCfZBE9IaB?p=preview

变化文本字段值的拨弄它也会影响ng-repeat

+0

@Sajeetharan检查我的小提琴,你会得到这个问题 – Jabaa

回答

5

值要指定这就是为什么它改变可变参考。你必须复制变量。下面是工作例如: -

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 to Edit {{item.name}} item</span> 
 
    </p> 
 
    
 
    
 
    <h2>Edit</h2> 
 
    <input type="text" ng-model="edit.name" /> 
 
</div>

+0

感谢您指出它工作正常,现在的问题 – Jabaa

+0

@Jabaa很高兴我能:-) –

+1

需要帮助等待8分钟以接受答案:) – Jabaa

相关问题