Javascript 如何使用angularjs向json数组的特定索引添加数据

Javascript 如何使用angularjs向json数组的特定索引添加数据,javascript,angularjs,json,Javascript,Angularjs,Json,我创建了表数据和编辑按钮,当我试图将编辑数据添加到特定行时遇到问题 这是我的扑克牌:- 这里是HTML页面 <table class="table table-bordered"> <thead> <tr> <th><input type='checkbox' ng-modal='isall' ng-click='s

我创建了表数据和编辑按钮,当我试图将编辑数据添加到特定行时遇到问题

这是我的扑克牌:-

这里是HTML页面

    <table class="table table-bordered">
                <thead>
                    <tr>
                        <th><input type='checkbox' ng-modal='isall' ng-click='selectallrows()'></th>
                        <th>ID</th>
                        <th>Name</th>
                        <th>EDIT</th>

                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat='p in person' ng-class="{'success':tableselection[$index]}">
                        <td><input type='checkbox' ng-model='tableselection[$index]'></td>
                        <td>{{p.id}}</td>
                        <td>{{p.name}}</td>
                        <td><button class='btn btn-primary' ng-click='edit(p.id)'><span class="glyphicon glyphicon-pencil"></span>Edit</button></td>
                    </tr>
                </tbody>

            </table>
<div ng-show='editabledata'>
      <form role='form' class='form-horizontal'>

        <div class='form-group'>
      <label>Id :-</label>
      <input type='text' ng-model='pid' class='from-group col-sm-2'></div>
      <div class='form-group'>
      <label>Name:-</label>
      <input type='text' ng-model='pname' class='from-group col-sm-2'>
      </div>
      <button class='btn btn-primary' ng-click='saveeditdata(pid)'>Save</button>
      <button clas='btn btn-primary' ng-click='canceleditdata()'>Cancel</button>
      </form>
    </div>

身份证件
名称
编辑
{{p.id}}
{{p.name}}
编辑
身份证:-
姓名:-
拯救
取消
这是我的.js

$scope.person=[
        {id:1,name:'devendher'},
        {id:2,name:'Macha'},
        {id:3,name:'Macha devendher'}
        ];

    $scope.editabledata=false;
  $scope.edit=function(id){
    $scope.editabledata=true;

   for(i=0;i<$scope.person.length;i++){

     if($scope.person[i].id==id)
   {
     $scope.pid=$scope.person[i].id;
     $scope.pname=$scope.person[i].name;
   }

   }
  }


  $scope.saveeditdata=function(id){
    for(i=0;i<$scope.person.length;i++){
      if($scope.person[i].id==id){
        $scope.person[i].push({'id':$scope.pid,'name':$scope.pname})
      }
    }


  }
$scope.person=[
{id:1,名称:'devendher'},
{id:2,名称:'Macha'},
{id:3,姓名:'Macha devendher'}
];
$scope.editabledata=false;
$scope.edit=函数(id){
$scope.editabledata=true;
对于(i=0;i我想你的意思是:

$scope.person.push({...})

我想你的意思是:

$scope.person.push({...})


这是非常复杂的,您没有利用表单数据使用单个对象的优势。此外,您正在使用不同的属性名称来编辑和原始对象。您正在执行的所有循环都是不必要的

更简单的步骤:

要编辑对象,请将整个对象传递给控制器函数:

  <button  ng-click='edit(p)'>
在编辑表单中,使用
ng model
中的完整对象引用。无需创建单个变量,使用已存在的属性

ID :  <input ng-model="editItem.id">
Name: <input ng-model="editItem.name">
有关删除,请参见:


这太复杂了,您没有利用表单数据使用单个对象的优势。此外,编辑对象和原始对象使用不同的属性名称。您正在执行的所有循环都是不必要的

更简单的步骤:

要编辑对象,请将整个对象传递给控制器函数:

  <button  ng-click='edit(p)'>
在编辑表单中,使用
ng model
中的完整对象引用。无需创建单个变量,使用已存在的属性

ID :  <input ng-model="editItem.id">
Name: <input ng-model="editItem.name">
有关删除,请参见:


是的,非常感谢:)是的,非常感谢:)