从ng-repeat编辑项目

时间:2016-02-29 07:59:46

标签: html angularjs ng-repeat

到目前为止,我已成功创建了一个从ng-repeat中删除元素并切换表单的函数。我无法弄清楚如何在ng-repeat中编辑给定元素。

理想情况下,我想点击元素,并让表单显示输入中已有的值。因此,所有用户需要做的是编辑所需的任何字段,单击“提交”,然后将新更新的项目添加回最初的数组。

这就是我想出来的:

Y

normalization<- function(testsample){
newSample<-data.frame(1:nrow(testSample))
for(j in 1:ncol(testsample)){
  mu<-mean(testsample[,j])
  sigma<-sd(testsample[,j])
  colName<- names(testsample)
      for(i in 1:nrow(testsample)){
        newSample$colName[j] <- transmute(testsample,colName[j]=((testsample[i,j]-mu)/sigma)           

     }
  }

print(newSample)
return(newSample)
}
z<-normalization(testsample)

2 个答案:

答案 0 :(得分:3)

您需要告诉要编辑的哪个项目。所以它应该是

-

然后,此函数应存储该项目的副本,以便在某个变量中进行编辑:

<span ng-click="vm.edit(item)">E</span>

表单应绑定到此项目副本:

vm.edit= function(item) {
  vm.editedItem = angular.copy(item);
};

然后save方法应找回数组中的原始项(由于其ID或索引),并从<input type="text" ng-model="vm.editedItem.name"/> <input type="text" ng-model="vm.editedItem.desc"/> 复制已编辑的字段。

答案 1 :(得分:0)

&#13;
&#13;
angular
  .module('testApp', [])
  .controller('testCtrl', testCtrl)
  .service('testSrvc', testSrvc);

function testCtrl(testSrvc) {
  var vm = this;
  vm.list = testSrvc.list;
  this.index1 = -1;
  vm.removeItem = function(idx) {
    testSrvc.remove(idx);
  }

  vm.showEditForm = false;
  vm.toggleEditForm = function(item,index) {
     this.show = true;
     this.index1 = index;
  };

  vm.update = function(item,index) {
    vm.list[index].name = item.name;
     vm.list[index].desc = item.desc;
    this.index1 = -1;
  }
}

function testSrvc() {
  this.show = false;
  this.list = [
    {
      name: 'asdf',
      desc: 'lorem ipsum dolor'
    },
    {
      name: 'qwerty',
      desc: 'lorem ipsum amet'
    }
  ];

  this.remove = function(itemIndex) {
    this.list.splice(itemIndex, 1);
  };

  this.edit = function(index) {
    this.show = true;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp">
  <div ng-controller="testCtrl as vm">
    <form ng-show="vm.showEditForm">
      
    </form>
    <table>
      <tr>
        <th>Name</th>
        <th>Description</th>
      </tr>
      <tr ng-repeat="item in vm.list" >
        <td>
          {{item.name}}
        </td>
        <td>
          {{item.desc}}
          <span ng-click="vm.toggleEditForm(item,$index)">E</span>
          <input ng-show="vm.index1 == $index" type="text" ng-model="item.name"  />
          <input ng-show="vm.index1 == $index" type="text" ng-model="item.desc" />
            <button ng-show="vm.index1 == $index" ng-click="vm.update(item,$index)">Submit</button>
          <span ng-click="vm.removeItem($index)">X</span>
        </td>
      </tr>
    </table>
  </div>
</div>
&#13;
&#13;
&#13;