sun*_*sun 12 angular angular-reactive-forms angular7
嗨,大家好我是新手。实际上,我正在尝试从服务中订阅数据,然后将该数据传递给我的表单控件(例如,它就像一个编辑表单)。
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormArray, FormControl } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { QuestionService } from '../shared/question.service';
@Component({
selector: 'app-update-que',
templateUrl: './update-que.component.html',
styleUrls: ['./update-que.component.scss']
})
export class UpdateQueComponent implements OnInit {
questionsTypes = ['Text Type', 'Multiple choice', 'Single Select'];
selectedQuestionType: string = "";
question: any = {};
constructor(private route: ActivatedRoute, private router: Router,
private qService: QuestionService, private fb: FormBuilder) {
}
ngOnInit() {
this.getQuebyid();
}
getQuebyid(){
this.route.params.subscribe(params => {
this.qService.editQue([params['id']]).subscribe(res =>{
this.question = res;
});
});
}
editqueForm = this.fb.group({
user: [''],
questioning: ['', Validators.required],
questionType: ['', Validators.required],
options: new FormArray([])
})
setValue(){
this.editqueForm.setValue({user: this.question.user, questioning: this.question.questioning})
}
}
Run Code Online (Sandbox Code Playgroud)
如果我[(ngModule)]在表单字段中使用该值将其设置为元素,则它可以正常工作并显示警告,它将在angular 7版本中弃用。
<textarea formControlName="questioning" [(ngModule)]="question.questioning" cols="70" rows="4"></textarea>
Run Code Online (Sandbox Code Playgroud)
因此,我通过以下操作将值设置为表单控件,但该元素未显示这些值。
setValue(){
this.editqueForm.setValue({user: this.question.user, questioning: this.question.questioning})
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何设置值来挖掘反应形式。请给我建议。
Nad*_*net 56
在Reactive Form 中,有两种主要的解决方案来更新表单字段的值。
在构造函数中初始化模型结构:
this.newForm = this.formBuilder.group({
firstName: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(8)]],
lastName: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(8)]]
});
Run Code Online (Sandbox Code Playgroud)如果要更新表单的所有字段:
this.newForm.setValue({
firstName: 'abc',
lastName: 'def'
});
Run Code Online (Sandbox Code Playgroud)如果要更新表单的特定字段:
this.newForm.controls.firstName.setValue('abc');
Run Code Online (Sandbox Code Playgroud)注意: 必须为 FormGroup 中的所有表单字段控件提供完整的模型结构。如果您错过了任何属性或子集集合,则会抛出异常。
如果您想更新表单的某些/特定字段:
this.newForm.patchValue({
firstName: 'abc'
});
Run Code Online (Sandbox Code Playgroud)注意: 为 FormGroup 中的所有/任何表单字段控件提供模型结构不是强制性的。如果您错过任何属性或子集集合,则不会抛出任何异常。
wen*_*jun 19
设置或更新反应性表单可以使用patchValue和setValue来完成表单控制值。但是,在某些情况下使用patchValue可能更好。
patchValue不需要在参数中指定所有控件即可更新/设置表单控件的值。另一方面,setValue要求填写所有“表单控件”值,如果未在参数中指定任何控件,它将返回错误。
在这种情况下,我们将要使用patchValue,因为我们仅更新user和questioning:
this.qService.editQue([params["id"]]).subscribe(res => {
this.question = res;
this.editqueForm.patchValue({
user: this.question.user,
questioning: this.question.questioning
});
});
Run Code Online (Sandbox Code Playgroud)
编辑:如果您想做一些ES6的Object Destructuring,您可能有兴趣这样做
const { user, questioning } = this.question;
this.editqueForm.patchValue({
user,
questioning
});
Run Code Online (Sandbox Code Playgroud)
塔达!
TAB*_*TAB 10
尝试这个。
\neditqueForm = this.fb.group({\n user: [this.question.user],\n questioning: [this.question.questioning, Validators.required],\n questionType: [this.question.questionType, Validators.required],\n options: new FormArray([])\n})\nRun Code Online (Sandbox Code Playgroud)\nsetValue() 和 patchValue()
\n如果你想设置一个控件的值,这是行不通的,因此你必须设置两个控件的值:
\nformgroup.setValue({name: \xe2\x80\x98abc\xe2\x80\x99, age: \xe2\x80\x9825\xe2\x80\x99});\nRun Code Online (Sandbox Code Playgroud)\n有必要提及该方法内的所有控件。如果不这样做,就会抛出错误。
\n另一方面patchvalue(),这部分要容易得多,让\xe2\x80\x99s 说你只想将名称分配为新值:
formgroup.patchValue({name:\xe2\x80\x99abc\xe2\x80\x99});\nRun Code Online (Sandbox Code Playgroud)\n
“通常”的解决方案是创建一个返回空 formGroup 或完整 formGroup 的函数
createFormGroup(data:any)
{
return this.fb.group({
user: [data?data.user:null],
questioning: [data?data.questioning:null, Validators.required],
questionType: [data?data.questionType, Validators.required],
options: new FormArray([this.createArray(data?data.options:null])
})
}
//return an array of formGroup
createArray(data:any[]|null):FormGroup[]
{
return data.map(x=>this.fb.group({
....
})
}
Run Code Online (Sandbox Code Playgroud)
然后,在 SUBSCRIBE 中调用该函数
this.qService.editQue([params["id"]]).subscribe(res => {
this.editqueForm = this.createFormGroup(res);
});
Run Code Online (Sandbox Code Playgroud)
请小心!,您的表单必须包含 *ngIf 以避免初始错误
<form *ngIf="editqueForm" [formGroup]="editqueForm">
....
</form>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12536 次 |
| 最近记录: |