Javascript Angular 2动态表单-实现取消功能
我正在尝试实现取消功能,该功能应恢复到表单中以前的值,这些值在上次提交表单时出现。我试图创建传递给表单的数据对象的副本,在提交函数中,我将新值替换为副本,在取消函数中,我将副本值替换为原始值。但是当我调用cancel函数时,我没有得到前面的值 有错误的工作代码: 我基于Angualr 2表单的模板驱动代码实现了取消功能: 类型脚本类代码:Javascript Angular 2动态表单-实现取消功能,javascript,forms,typescript,angular,angular2-forms,Javascript,Forms,Typescript,Angular,Angular2 Forms,我正在尝试实现取消功能,该功能应恢复到表单中以前的值,这些值在上次提交表单时出现。我试图创建传递给表单的数据对象的副本,在提交函数中,我将新值替换为副本,在取消函数中,我将副本值替换为原始值。但是当我调用cancel函数时,我没有得到前面的值 有错误的工作代码: 我基于Angualr 2表单的模板驱动代码实现了取消功能: 类型脚本类代码: import { Component, Input, OnInit } from '@angular/core'; import { FormGroup,
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");
}
}
从'@angular/core'导入{Component,Input,OnInit};
从'@angular/forms'导入{FormGroup,REACTIVE_FORM_directions};
从“/question base”导入{QuestionBase};
从“./question-control.service”导入{QuestionControlService};
@组成部分({
选择器:“动态表单”,
templateUrl:'app/dynamic form.component.html',
指令:[反应型指令],
提供者:[问题控制服务]
})
导出类DynamicFormComponent在NIT上实现{
@输入()问题:问题库[]=[];
表格:表格组;
有效载荷:目标;
问题t:问题库;
构造函数(专用qcs:QuestionControlService){}
恩戈尼尼特(){
this.form=this.qcs.toFormGroup(this.questions);
log(“forminit”,this.questions);
this.question t=this.questions;
}
onSubmit(){
this.payLoad=JSON.stringify(this.form.value);
this.payLoad2=this.payLoad2;
this.question t=this.questions;
console.log(“原始数据”,这个问题);
console.log(“重复数据”,this.questiont);
}
取消{
this.questions=this.questiont;
console.log(“原始数据”,这个问题);
console.log(“重复数据”,this.questiont);
控制台日志(“已取消”);
}
}
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>
{{question.label}
{{opt.value}}
{{question.label}}是必需的
拯救
取消
保存了以下值
{{payLoad}
任何人遇到此问题或试图实现此操作。表单重置将在RC5中实现 您的方法不适用于对象:
this.questiont=this.questions; //you are sharing refrence to the same object
您应该改为克隆对象(有很多方法可以做到这一点,我使用的是JSON):
表格重置将在RC5中实施 您的方法不适用于对象:
this.questiont=this.questions; //you are sharing refrence to the same object
您应该改为克隆对象(有很多方法可以做到这一点,我使用的是JSON):
谢谢你的回答,它起作用了。我正在尝试实现clear功能,它暂时会清除所示表单中的所有值,以便用户可以输入全新的数据。用上面的代码可以吗?你能解释一下这个清晰的功能吗。谢谢你有什么想法,如何实现清晰的功能?过来帮我。谢谢。目前您需要创建模型副本以实现清晰,与我的回答中的方法相同。工作正常!angular是否有任何内置方式来处理此功能?谢谢您的回答,它正在工作。我正在尝试实现clear功能,它暂时会清除所示表单中的所有值,以便用户可以输入全新的数据。用上面的代码可以吗?你能解释一下这个清晰的功能吗。谢谢你有什么想法,如何实现清晰的功能?过来帮我。谢谢。目前您需要创建模型副本以实现清晰,与我的回答中的方法相同。工作正常!angular是否有处理此功能的内置方式?