为什么在angular中不删除row



你能告诉我为什么我的行没有被删除吗?我做了一个演示,我添加了学生的名字。我能够添加学生的名字。现在我有两个问题,我不能删除行和编辑。你能告诉我哪里错了吗?

是我的演示http://plnkr.co/edit/1lem6t4h7b6Eefsz32t6?p=preview

app.controller("studentcntr",['$scope',function(scope){
    scope.studentDetail=[];
    scope.addStudent=function(){
        bootbox.prompt("Enter Name!",function(res){
            console.log(res);
            if(res==null){
            }else {
                scope.studentDetail.push({ name: res });
            }
            scope.$digest();
        });

    };
    scope.editRowName=function (name) {
      // body...
     // alert(name);
     setTimeout(function() {
      $('.bootbox-input').val(name); 
     }, 10);
    }
     scope.deleteRow=function (id) {
      // body...
      alert('s'+id)
      $('#'+id).remove();
    }


}])

我可以删除行,但问题是,当我删除行,并添加新的名称,它创建再次删除行为什么?为什么现在是永久删除

为什么现在是永久删除

因为你没有从scope.studentDetail中删除它,所以它是持久的。

变化

    <td ><button ng-click="editRowName(student)">Edit</button></td>
    <td ><button ng-click="deleteRow($index)" >Delete</button></td>
脚本

scope.editRowName=function (student) {
  bootbox.prompt({
    title: "What is your name?", //Title
    value: student.name, //Pre-filled name
    callback: function(result) {
      student.name = result; //Update name
      scope.$digest();
    }
  });
}
scope.deleteRow=function ($index) {
  scope.studentDetail.splice($index, 1);
}

问题是您正在从DOM中删除元素,而不是从数据模型中删除元素。

http://plnkr.co/edit/a4WLy1ckQHT68uz1CGeh?p =

预览

可用于编辑和删除。

<td><button ng-click="editRowName($index, student.name)">Edit</button></td>
<td><button ng-click="deleteRow($index)" >Delete</button></td>

和控制器

scope.editRowName=function (index, name) {
    scope.currentIndex = index;
   bootbox.prompt("Enter New name for " + name,function(res){
        console.log(res);
        if(res==null){
        }else {
          if (scope.currentIndex === -1) {
            scope.studentDetail.push({ name: res });
          } else {
            scope.studentDetail[scope.currentIndex].name = res;
            scope.currentIndex = -1;
          }
        }
        scope.$digest();
    });
}
 scope.deleteRow=function (id) {
  scope.studentDetail.splice(id, 1);
}

最新更新