大家好我是新来的.实际上,我正试图从一个服务订阅数据,而这些数据,我正在从传递给我的表单控制(例如,它就像一个编辑表单).
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})
}
}
如果我在表单字段中使用[(ngModule)]
来设置元素的值,它工作正常,并显示警告,在angular 7版本中,它将被弃用.
<textarea formControlName="questioning" [(ngModule)]="question.questioning" cols="70" rows="4"></textarea>
因此,我通过执行下面的操作来设置表单控件的值,但是元素没有显示这些值.
setValue(){
this.editqueForm.setValue({user: this.question.user, questioning: this.question.questioning})
}
谁能告诉我如何为我的react 形式设置值吗.请推荐我.