0
我有一个我从后端获得的兴趣列表。我希望用户能够选择他们想要的兴趣。我将只存储他们在数据库中检查的兴趣,并在他们加载页面时进行预填充。但首先,我需要获得用户选择的这些兴趣。角+材料 - 如何处理与表单组的多个复选框
interest.component.ts
export class InterestsComponent implements OnInit {
interestFormGroup : FormGroup
interests:any;
selected: any;
constructor(public interestService: InterestService, private formBuilder: FormBuilder,
) { }
ngOnInit() {
this.interestFormGroup = this.formBuilder.group({
interests: this.formBuilder.array([])
});
this.interestService.all().subscribe((res) => {
this.interests = res;
});
}
change() {
console.log(this.interestFormGroup.value);
}
}
interest.component.html
<div id="interest">
<div class="interest-list">
<form [formGroup]="interestFormGroup">
<div *ngFor="let interest of interests" >
<mat-checkbox class="example-margin" formNameArray="interests" (change)="change()">{{interest}}</mat-checkbox>
</div>
</form>
</div>
</div>
在我关于改变事件的console.log它示出了没有被添加到利益阵列内的值的interestFormGroup。即使是复选框也会被检查。
从stackblitz那不是我想要的,它返回true数组,虚假的价值观,当我真正想要的价值的利益。由于真值和假值实际上并不告诉我什么兴趣被选中,因为关键是指数。我甚至不知道为什么在这里添加索引?看起来像一个过于复杂的解决方案的东西本来是简单的。 – 2017-10-15 08:28:07
所以在那个stackbltiz示例中,它应该返回值为interest1,interest2,interest3而不是0的兴趣数组:true 1:true 2:true – 2017-10-15 08:33:43
您是否尝试提交表单?看看控制台 – yurzui