如何更新 angular js 中的 ng-repeat 值?

How to update ng-repeat values in angular js?

我是 angular js 的新手,我有一个数组,我正在通过 ng-repeat 指令循环它,并且我已经编写了用于复制、删除和编辑数组中值的代码。

如果我想删除或复制我可以做到,完成了吗? 但是如果我点击编辑,那里会出现一个弹出框,我想编辑这些更新值应该在数组中更新的值。

我怎样才能完成它?

<!doctype html>
<html>
<head>
 <title>Angular app</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js">
  </script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style type="text/css">
  .listv{
     margin-bottom: 30px;
  }
  .editpopup{
     width: 250px;
     height: 250px;
     border: 1px solid black;
     display: none;
     position: absolute;
     top: 0px;
     left: 0px;
     bottom: 0px;
     right: 0px;

     background-color:gray;
  }
  .editpopup-true{
     display: block;
  }
  .editpopup-false{
     display: none;
  }
  </style>
</head>
 <body ng-app="myApp">
    <div ng-controller="myCon">
     <div ng-repeat="s in items" class="listv">
        <span>{{s.id}}</span>
        <span>{{s.pname}}</span>
        <button ng-click="removeStudent($index)">remove</button>
        <button ng-click="copyrow($index)">copy</button>
        <button ng-click="editrow($index)">edit</button>
     </div></br>

     <div class="editpopup editpopup-{{istrue}} ">
        <p>edit id:<input type="text" ng-model="editedid"></p>
        <p>edit pname:<input type="text" ng-model="editedname"></p>
        <button ng-click="save($index)">save</button>
        <button ng-click="closepopup()">cancel</button>
     </div>

  </div>            

      var myApp=angular.module('myApp',[]);
      myApp.controller('myCon',function($scope){
      $scope.items=[{id:1,pname:'box1'},{id:2,pname:'box2'}, {id:3,pname:'box3'}];

    $scope.removeStudent=function($index){
      $scope.items.splice($index,1);
    }
  $scope.copyrow=function($index){

     $scope.len=$scope.items.length;
     $scope.ids=$scope.items[$index].id;
     $scope.pnames=$scope.items[$index].pname

     $scope.items.push({
          id:$scope.len+1,
          pname:$scope.pnames 
      });
  }
  $scope.editrow=function($index){
     $scope.istrue=true;
     $scope.editedid=$scope.items[$index].id;
     $scope.editedname=$scope.items[$index].pname;
  }
  $scope.closepopup=function(){
     $scope.istrue=false;

  }
  $scope.save=function($index){
     $scope.istrue=false;
     $scope.s.name=$scope.editedname;
  }
 });

这里是jsfiddle

最简单的方法是在单击编辑时使用 angular.copy 创建对象的克隆,然后在单击保存时将数据复制到数组中的项目。

首先初始化$scope.editedItem

$scope.editedItem = {};

对于editrow,我们将当前编辑的索引存储在$index中,然后将数据克隆到$scope.editedItem

$scope.editrow = function($index){
    $scope.istrue = true;
    $scope.$index = $index;
    angular.copy($scope.items[$index], $scope.editedItem);
}

然后在save我们将数据克隆回数组中的对象:

$scope.save = function(){
    $scope.istrue = false;
    angular.copy($scope.editedItem, $scope.items[$scope.$index]) 
}

需要更新视图以改为使用 editedItem:

<div class="editpopup editpopup-{{istrue}} ">
    <p>edit id:<input type="text" ng-model="editedItem.id"></p>
    <p>edit pname:<input type="text" ng-model="editedItem.pname"></p>
    <button ng-click="save()">save</button>
    <button ng-click="closepopup()">cancel</button>
 </div>

JsFiddle

首先,像这样保存旧值:

$scope.editrow=function($index){
    $scope.istrue=true;
    $scope.oldValue = $scope.items[$index].id; // save the old id
    $scope.editedid=$scope.items[$index].id;
    $scope.editedname=$scope.items[$index].pname;
};

然后,当您保存时,只需借助旧值找到正确的对象并像这样分配新值:

$scope.save=function($index){
    $scope.istrue=false;
    $scope.items.forEach(function (item) {
       if(item.id == $scope.oldValue){
           item.id = $scope.editedid;
           item.pname = $scope.editedname;
       }
    });
};

JsFiddle

我遇到了同样的问题。这是我的解决方案


我没有更新对象的原始代码

<div class="info" ng-repeat="email in vm.contactData.emails.other">
      <input type="text" ng-model="email" />
</div> 

使用项目 $index 正确绑定它

<div class="info" ng-repeat="email in vm.contactData.emails.other">
      <input type="text" ng-model="vm.contactData.emails.other[$index]" />
</div> 

此绑定会导致重新绘制问题,因为它不知道数组中更新的项目是否引用了之前位于该位置的同一项目。要解决此问题,我必须对数组的格式进行轻微更改。

['email1@google.com','email2@google.com']

变成

[
   {'email': 'email1@google.com'},
   {'email': 'email2@google.com'}
]

<div class="info" ng-repeat="email in vm.contactData.emails.other">
     <input type="text" ng-model="vm.contactData.emails.other[$index]" />
</div> 

变成

<div class="info" ng-repeat="email in vm.contactData.emails.other">
      <input type="text" ng-model="vm.contactData.emails.other[$index].email" />
</div> 

进行这些更改后,您应该有一个正确的绑定,不会出现输入字段失去焦点的任何重绘问题。

首先声明变量 $scope.getIndex=0; 并在单击保存按钮时从项目数组中获取索引。然后将 $index 分配给该变量。

现在您可以 items[$scope.getIndex] 在控制器的任何地方。并帮助您更新项目数组:

$scope.getIndex=0;
$scope.editrow=function($index){
     $scope.getIndex=$index;
     $scope.istrue=true;
     $scope.editedid=$scope.items[$index].id;
     $scope.editedname=$scope.items[$index].pname;
}

$scope.save=function($index){
    $scope.istrue=false;
    $scope.items[$scope.getIndex].id=$scope.editedid;
    $scope.items[$scope.getIndex].pname=$scope.editedname;
}[enter link description here][1]


JSFiddle