javascript - 使用 Angular 从 *ngFor 中删除 *ngFor 中的项目

标签 javascript angular typescript

我想从 *ngFor 中删除 *ngFor 中的项目。

enter image description here

当我删除回复“test2”时,

enter image description here

在我添加其他回复后,“test3”变为空。

enter image description here

<hello name="{{ name }}"></hello>

<form #form="ngForm" (ngSubmit)="submit()" ngNativeValidate class="mt-4">

  <div *ngFor="let content of contents; let indexContent = index; let firstContent = first;">

    <div *ngFor="let message of content.messages; let indexMessage = index; let firstMessage = first;">
      <table>
        <thead>
        <tr>
          <th>Id</th>
          <th>Text</th>
          <th class="text-right">Action</th>
        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let reply of message.replies; let indexReply = index; let firstReply = first;">
          <td [innerHtml]='reply.id'></td>
          <td>
            <input type="text"
            id="{{indexContent}}-{{indexMessage}}-{{indexReply}}-reply-text"
            [(ngModel)]=content.messages[indexMessage].replies[indexReply].text
            name="[[indexContent]]-[{{indexMessage}}]-[{{indexReply}}]-reply-text">
            <br>
            <span [innerHtml]="contents[indexContent].messages[0].replies[indexReply].text"></span>
          </td>
          <td>
            <span (click)="message.removeReply(reply)">Remove Reply</span>
          </td>
        </tr>
        </tbody>
      </table>

      <br>

      <span (click)="message.addNewReply()">
        Add Reply
      </span>
    </div>

  </div>
  <br>
  <button type="submit" class="btn btn-primary">Save</button>
</form>

我的消息模型具有添加回复、删除回复的不同功能

message.model.ts

import { Reply } from "./reply";

export class Message {

    constructor(public id: number         = 0,
                public text: string       = '',
                public replies: any[]     = []) {
    }

    public setModel(obj) {
        Object.assign(this, obj);
    }

    addReply(new_reply) {
        this.replies.push(new_reply);
    }

    addNewReply() {
        let new_reply = new Reply();

        this.replies.push(new_reply);
    }

    removeReply(reply) {
      this.replies.splice(this.replies.indexOf(reply), 1);
    }
}

我在这里重现了我的问题:Remove object from array in *ngFor in Angular

https://stackblitz.com/edit/angular-clmi7d

最佳答案

我会使用 trackBy 选项来避免 unexpected situations

html

<tr *ngFor="let reply of message.replies; trackBy: trackByFn; 
                                          ^^^^^^^^^^^^^^^^^^

app.component.ts

trackByFn(i: number) { 
  return i
}

关于javascript - 使用 Angular 从 *ngFor 中删除 *ngFor 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696165/

相关文章:

javascript - 如何从字符串中获取奇数和偶数位置的字符?

javascript - 欧芹远程验证

angular - 使用 Router.navigate() 后未调用 Observable.subscribe()

java - 访问下拉列表中选定的数据

typescript - 使用 JSPM 和 Typescript 改进 Webstorm 11 中的导入

node.js - 安装Visual Studio 2017如何破坏Node.js或Typescript编译器?

javascript - 如何过滤掉复杂查询中的文本字段?

javascript - 如何通过 JavaScript 访问 HTTP 请求 header 字段?

javascript - 如何在 Angular 中按日期字符串(昨天、今天、本月、上个月)进行过滤

reactjs - 尽管 TypeScript 编译器出现错误,为什么我的 React Native 应用程序仍能成功构建?