javascript - 从 ng-bootstrap 表中删除行

标签 javascript angular typescript ng-bootstrap

我已经成功实现了 ng-bootstrap 表完整示例。
从 DOM 和数据库中删除对象正在工作,但我找不到从 View 中删除行的方法。为了查看更改,需要重新加载页面。请注意,删除功能是并且应该从 ng-bootstrap 模式对话框确认按钮触发。

我无法像下面的方法或类似方法那样从 for 循环中调用 data$ ,因为 todo(或任何其他方法) ) 是可观察的 todos$,

<!-- html -->
<tr *ngFor="let todo of tableService.todos$ | async;">
// typescript
deleteRow(id){
  for(let i = 0; i < this.data.length; ++i){
    if (this.data[i].id === id) {
        this.data.splice(i,1);
    }
  }
}

请有人给我指出正确的方向。

我有这段代码:

deleteTodo(id: string) {
  this.todoService.deleteTodo(id)
    .subscribe(data => {
        console.log(data); // print message from server
    },
        error => console.log(error)
    );
  this.tableService.todoArray = this.tableService.todoArray.filter(elem => elem._id !== id);
  this.todoLength--;
  this.modalService.dismissAll();
  console.log('filtered array: ' + this.tableService.todoArray.length);
  console.log('id: ' + id);
}

此函数从数据库中删除待办事项,过滤器方法从数组中删除待办事项。请查看下面的屏幕截图。

enter image description here

存储到我的应用程序源代码:
https://github.com/SrdjanMilic/NG-Bootstrap-Todo-list

最佳答案

Angular 变化检测不适用于splice,因为它不会更改对数组变量的引用。您需要更新变量引用(下面的示例)或 trigger change detection manually .

deleteRow(id) {
   this.data = this.data.filter(elem => elem.id !== id);
}

关于javascript - 从 ng-bootstrap 表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60633192/

相关文章:

angular - 时间线模板会是什么样子?

javascript - ReactJS 无法读取未定义的属性 'bind'

javascript - Suitescript,nlapiRequestURL,无法将 JSON 从 URL 转换为对象/数组

Angular 2/Express 上传文件到服务器

ngFor 循环中点击的 div 的 Angular Return id

javascript - 将 Stackblitz HelloComponent 转变为 Web 组件?

javascript - 如何使用 Typescript 2.4 的 import() 方法动态获取entryComponents

javascript - 如何将一个div的内容复制到另一个div

typescript - 创建以前的类型

javascript - Angular 8 - 延迟加载模块 : Error TS1323: Dynamic import is only supported when '--module' flag is 'commonjs' or 'esNext'