Javascript 选择的被动表单选项是formcontrol中的对象
在我的组件中,我有以下成员存储:Javascript 选择的被动表单选项是formcontrol中的对象,javascript,angular,Javascript,Angular,在我的组件中,我有以下成员存储: stores : Store[] 存储是以下接口: export interface Store{ store_name: string; owner: string; display_name?: string; } 我的html中有以下内容: <select [formControlName]="stores" > <option value="null" hidden s
stores : Store[]
存储是以下接口:
export interface Store{
store_name: string;
owner: string;
display_name?: string;
}
我的html中有以下内容:
<select [formControlName]="stores" >
<option value="null" hidden selected>Select</option>
<ng-container *ngIf="selectedProduct">
<option *ngFor="let store of stores" [value]="store">
{{ store.display_name }}
</option>
</ng-container>
</select>
为什么在我的onSubmit方法中,当我尝试访问所选存储的字段时,我会变得未定义
onSubmit(){
console.log(this.fg.get("stores").value.store_name); // undefined
.....
}
顺便说一句:使用角度10。使用
[ngValue]
而不是[value]
?@JasonWhite它起作用了,你能解释一下原因吗?我通过谷歌搜索了解了区别,tnx:)两者之间的唯一区别是值始终是字符串,在ngValue中可以传递的对象[value]
始终是字符串,如果要绑定对象,则需要使用[ngValue]
。
onSubmit(){
console.log(this.fg.get("stores").value.store_name); // undefined
.....
}