【发布时间】:2018-07-11 20:19:03
【问题描述】:
我有一个使用 ReactiveForms 的组件正在使用:
<form [formGroup]="generalDataForm" (ngSubmit)="onSubmit()">
<select name="grade_id" id="grade_id" class="form-control" formControlName="grade_id">
<option *ngFor="let grade of grades" [ngValue]="grade.id [selected]="grade.id === user.grade_id">
{{ grade.text | translate }} {{ user.grade_id }} {{ user.grade_id === grade.id }}
</option>
</select>
<div align="right">
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
在 mock_grades.ts 中,我有:
export const GRADES: Grade[] = [
{
'id': 0,
'text': 'add_cat.no_grade_restriction',
'order': 0,
},
{
'id': 2,
'text': '7 Kyu',
'order': 1,
}, etc...
在我的组件中,我有:
export class ProfileComponent implements OnInit {
generalDataForm: FormGroup;
public type = 'component';
grades = GRADES;
...
ngOnInit(): void {
this.generalDataForm = this.formBuilder.group({
name: [this.user.name, Validators.required],
firstname: [''],
lastname: [''],
grade_id: [''],
country_id: ['']
}
}
}
我可以让值列表确定,但我无法选择所选项目。
很奇怪,因为我在每个item上都打印了condition结果,应该选中的时候是true,但是却没有选中item...
我错过了什么?
【问题讨论】:
标签: angular angular6 angular-reactive-forms