我正在尝试为 Angualr 2 动态表单实现 Clear 功能,该功能应将所有值清除为 null。但是当我尝试使用 this.questions[i].value="null";
或 this.questions[i].value="undefinded";< 时出现错误
或 this.questions[i].value='';
替换表单中的每个值。
工作代码:http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?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 = JSON.parse(JSON.stringify(this.questions));
}
onSubmit() {
this.payLoad = JSON.stringify(this.form.value);
this.payLoad2=this.payLoad;
this.questiont = JSON.parse(JSON.stringify(this.questions));
}
cancel(){
console.log("Canceled");
this.questions = JSON.parse(JSON.stringify(this.questiont));
}
clear(){
for(var i=0;i<this.questions.length;i++){
this.questions[i].value='';
}
console.log("Cleared");
}
}
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>
<button type="button" class="btn btn-default" (click)="clear()">Clear</button>
</div>
</form>
<div *ngIf="payLoad" class="form-row">
<strong>Saved the following values</strong><br>{{payLoad}}
</div>
</div>
有什么方法可以在不出错的情况下完成。
最佳答案
问题在于您的逻辑检查并显示消息(如果需要字段)。
你的笨蛋代码抛出异常 Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'
问题是它会使用 questions[i].value 的值运行模板代码,但一旦您调用clear,它就会发生变化。 Angular 比较 questions[i].value 并注意到它与开始渲染时不同,并认为它犯了错误。它还没有意识到您更改了该值。您必须让它知道值已更改。
如果您要在启用生产模式的情况下运行代码,它将不会运行此安全检查,并且您不会收到任何错误,但不要仅仅为了解决此问题而启用生产模式。
解决方案
修复方法是导入 ChangeDetectorRef
来自@angular/core
到dynamic.form.component.ts
添加private cdr: ChangeDetectorRef
到构造函数
并添加 this.cdr.detectChanges();
到你的 clear()
的末尾方法。
这会让 Angular 拾取知道发生了变化,并且它不会认为它犯了错误
P.S.罪魁祸首是
<div class="errorMessage" *ngIf=" form && !form.controls[question.key].valid">{{question.label}} is required</div>
和
group[question.key] = question.required ? new FormControl(question.value || '', Validators.required) : new FormControl(question.value || ''); });
关于javascript - Angular 2 动态表单 - 实现清晰的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732307/