javascript - Angular 2 动态表单 - 实现取消功能

标签 javascript forms typescript angular angular2-forms

我正在尝试实现取消功能,该功能应恢复为表单中的先前值,这些值在上次提交表单时存在。我正在尝试创建传递给表单的数据对象的副本,在提交函数中我将新值替换为副本,在取消函数中,我将副本值替换为原始值。但是当我调用取消函数时,我没有得到以前的值。

有错误的工作代码:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview

我基于 Angualr 2 表单的模板驱动代码实现了取消功能:http://plnkr.co/edit/LCAPYTZElQDjrSgh3xnT?p=preview

typescript 类代码:

import { Component, Input, OnInit }  from '@angular/core';
import { FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { QuestionBase }                 from './question-base';
import { QuestionControlService }       from './question-control.service';

@Component({
  selector: 'dynamic-form',
  templateUrl: 'app/dynamic-form.component.html',
  directives: [REACTIVE_FORM_DIRECTIVES],
  providers:  [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {

  @Input() questions: QuestionBase<any>[] = [];
  form: FormGroup;
  payLoad:object;
  questiont: QuestionBase<any>;
  constructor(private qcs: QuestionControlService) {  }
  ngOnInit() {
    this.form = this.qcs.toFormGroup(this.questions);
    console.log("Form Init",this.questions);
    this.questiont=this.questions;
  }
  onSubmit() {
    this.payLoad = JSON.stringify(this.form.value);
    this.payLoad2=this.payLoad;
    this.questiont=this.questions;
    console.log("Original Data",this.questions);
    console.log("Duplicate Data",this.questiont);
  }
  cancel(){
    this.questions=this.questiont;
    console.log("Original Data",this.questions);
    console.log("Duplicate Data",this.questiont);
    console.log("Canceled");
  }

}

HTML代码:

<div>
  <form [formGroup]="form">

    <div *ngFor="let question of questions" class="form-row">
      <label [attr.for]="question.key">{{question.label}}</label>

  <div [ngSwitch]="question.controlType">

    <input *ngSwitchCase="'textbox'" [formControlName]="question.key"
            [id]="question.key" [type]="question.type" [(ngModel)]="question.value">

    <select [id]="question.key" [(ngModel)]="question.value" *ngSwitchCase="'dropdown'" [formControlName]="question.key" >
      <option *ngFor="let opt of question.options" [ngValue]="opt.key" >{{opt.value}}</option>
    </select>

  </div> 
  <div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</div>
    </div>

    <div class="form-row">
      <button type="submit" [disabled]="!form.valid" (click)="onSubmit()">Save</button>
      <button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>

    </div>
  </form>

  <div *ngIf="payLoad" class="form-row">
    <strong>Saved the following values</strong><br>{{payLoad}}
  </div>
</div>

任何人都遇到过这个问题或试图实现这个问题。

最佳答案

表单重置将在 RC5 中实现。

您的方法不适用于对象:

this.questiont=this.questions; //you are sharing refrence to the same object

您应该改为克隆对象(有很多方法可以做到这一点,我使用的是 JSON):

this.questiont = JSON.parse(JSON.stringify(this.questions));

关于javascript - Angular 2 动态表单 - 实现取消功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38730913/

相关文章:

javascript - 是否可以在 Javascript/AngularJS 中同步并发调用?

javascript - Bootstrap 验证冲突

php - 尝试更新数值时出现语法错误 (MySQL)

html - 如何更改按钮 CSS 属性

node.js - typescript 错误——错误 : Cannot find module 'typescript/tsc.js'

javascript - 将 utf-8 字体添加到 jsPDF 库以在 Angular 应用程序中打印 utf-8 阿拉伯语 pdf

typescript - 限制方法中的接口(interface)类型参数

javascript - jQuery LightSlider 不工作,但在 JSFiddle 中工作得很好

不允许使用Django方法(POST)

javascript - 每 2 分钟自动单击网站上的一个按钮?