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

相關問題