【发布时间】:2019-02-19 09:17:02
【问题描述】:
我正在尝试从回复列表中为特定回复设置分数。最初,我尝试使用 input value 属性设置默认值。当我只提交默认值时,我收到空字符串。当我更改输入字段的值时,这些值会成功传递回函数。
这些来源 - here 和 here - 帮助我理解了为什么这不起作用。我在 .ts 文件中设置的空字符串或默认值会覆盖 .html 文件中的值。我也尝试使用 ngModel,但遇到了类似的问题。
当我有许多可以单独评分的回复时,我仍在努力理解如何设置默认值。谁能帮我理解在这种情况下如何设置默认值?
HTML 文件
<mat-card *ngFor="let reply of replies">
<mat-card-title>
{{reply.title}}
{{reply.user}}
</mat-card-title>
<mat-card-content>
{{reply.content}}
</mat-card-content>
<mat-card-actions>
<form [formGroup]="feedbackForm">
<div class="form-group">
<input matInput formControlName="id" placeholder="id" type="hidden" value="{{reply._id}}>
</div>
<div class="form-group">
<input matInput formControlName="grade" placeholder="Enter Grade" type="number" value="{{reply.grade}}>
</div>
<div>
<button type="submit" (click)="feedbackResponse">Submit</button>
</div>
</form>
</mat-card-actions>
</mat-card>
打字稿文件
public ngOnInit() {
this.replies = [{
"_id": "dkjldajl",
"user": "Person 1",
"title": "Person 1's Reply",
"content": "The content that Person 1 wrote.",
"grade": 0
}, {
"_id": "danlkanl",
"user": "Person 2",
"title": "Person 2's Reply",
"content": "The content that Person 2 wrote.",
"grade": 0
}]
this.feedbackForm = this._formBuilder.group({
id: ['', Validators.required],
grade: ['']
})
//How can I set the values?
this.feedbackForm.get('id').setValue(???)
this.feedbackForm.get('grade').setValue(???)
//
}
get id() {
return this.feedbackForm.get('id');
}
get grade() {
return this.feedbackForm.get('grade');
}
feedbackResponse() {
console.log(this.replyFeedbackForm)
}
【问题讨论】:
-
你需要创建formgroup数组
标签: angular angular6 angular-reactive-forms