html
<select class="form-control" id="venue" name="venue1"
formGroupName="venue"
(change)="venue_method($event.target.value)">
<option hidden >Click to choose a venue...</option>
<option [attr.value]="venue.id" *ngFor="let venue of venues">
{{venue.name}}
</option>
<input type="hidden" formControlName="title" name="title">
</select>
组件代码
this.rForm = fb.group({
'venue': fb.group({
'VenueId': [],
'title': [],
'isAvailable': [],
//some more values
}),
//some other controls and groups
});
venue_method(input: any) {
console.log(input)
this.rForm.get('venue').patchValue({ title: input.name });
this.rForm.get('venue').patchValue({ VenueId: input.id });
this.rForm.get('venue').patchValue({isAvailable: true });
//some more patches
console.log(this.rForm.get('venue').value);
}
我想发送我的 field 拥有的所有属性的值,例如“name”和“id”,但对于 attr 值,我只能修补一个值,对此正确的解决方法是什么?
注意:我不想选择增强显示所有选项,我想从“让 field 的 field ”访问“ field ”的所有属性。 field 有 field .名称和 field .id,目前我可以从我的“ field 方法”访问 field .id或 field .名称。如何同时访问两者?
最佳答案
您需要稍微更新一下您的代码,如下所示。更新您的
<option [attr.value]="venue.id" *ngFor="let venue of venues">
至
<option value="venue | json" *ngFor="let venue of venues">
<select class="form-control" id="venue" name="venue1"
formGroupName="venue"
(change)="venue_method($event.target.value)">
<option hidden >Click to choose a venue...</option>
<option value="venue | json" *ngFor="let venue of venues">
{{venue.name}}
</option>
<input type="hidden" formControlName="title" name="title">
</select>
在 TS 中,您需要 parse
您的数据再次创建对象。
关于angular - 如何从 Angular 中的选择选项获取多个属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51785646/