代码之家  ›  专栏  ›  技术社区  ›  Vladimir Humeniuk

取消时还原textarea值

  •  1
  • Vladimir Humeniuk  · 技术社区  · 6 年前

    在我的Angular 5项目中,我有文本区域,在那里我用保存/取消按钮绑定了一些值(注释编辑器),我想在用户单击“取消”按钮时恢复旧的文本区域值。

    模板:

    <textarea [(ngModel)]="comment.text"></textarea>
    
    <button (click)="cancelEdit(comment.text)">Cancel</button>
    

    组件:

    oldCommentValue: string;
    
    cancelEdit(comment) {
      comment = this.oldCommentValue;
    }
    

    但这并不能恢复旧的价值。我做错了什么?

    1 回复  |  直到 6 年前
        1
  •  3
  •   Stefan    6 年前

    在这种情况下,应将此值设置为局部变量

    this.comment.text = this.oldCommentValue;
    

    comment = this.oldCommentValue;