3 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
模板文件:您formNameControl的不正確:
你可以看看這個(gè)演示可能對你有幫助!
<form [formGroup]="entryForm" (ngSubmit)="onSubmit()">
<div class="lang">
<div class="lang-col">
<textarea formControlName="firstValue"> </textarea>
</div>
<button type="submit">Submit</button>
</div>
</form>
類文件
entryForm: FormGroup;
ngOnInit() {
this.entryForm = new FormGroup({
firstValue: new FormControl('Test...')
});
}
onSubmit() {
console.log(this.entryForm.value);
}
或者您可以檢查是否formControlName為空然后設(shè)置默認(rèn)值
onSubmit() {
if(this.entryForm.get('firstValue').value == "") {
this.entryForm.patchValue({
firstValue: 'Your defualt value goes here'
});
}
console.log(this.entryForm.value);
}

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊
作業(yè)中的問題。你必須在類定義之后分配你的輸入表單的值,像這樣
export class MyComponent implements OnInit{
public entryForm: FormGroup = new FormGroup({
firsValue: new formControl('')
});
constructor()
ngOnInit()
}
添加回答
舉報(bào)