[英]Angular 6 : Passing selected value of select box value to the component on ngSubmit
I am using Angular 6 along with angular material and i have the form as below我正在使用 Angular 6 和 angular 材料,我的表格如下
<form (ngSubmit)="addDetails(line.value, menu.value)" [formGroup]="addMealForm">
<mat-card class="mb-5 float-right w-100">
<mat-form-field class="ml-5 mr-5 w-25">
<mat-label>Menu</mat-label>
<mat-select>
<mat-option #menu *ngFor="let menu of menus" [value]="menu.value">
{{menu.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="ml-5 mr-5 w-25">
<mat-label>line</mat-label>
<mat-select>
<mat-option #line *ngFor="let line of lines" [value]="line.value">
{{line.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<button type="submit" mat-raised-button color="accent">
<mat-icon>add</mat-icon>
Add Meal
</button>
</mat-card>
</form>
I am trying to send the selected values from the dropdown to my component like below我正在尝试将下拉列表中的选定值发送到我的组件,如下所示
addDetails(line,menu) {
console.log(line);
console.log(menu);
}
for some reason i am not able to do this.由于某种原因,我无法做到这一点。 i am getting the below error
我收到以下错误
TypeError: Cannot read property 'value' of undefined TypeError:无法读取未定义的属性“值”
Can someone tell me why this is happening有人能告诉我为什么会这样吗
I'm assuming it's because your sending #line (which is the template-variable of the many mat-options) as opposed to sending the actual value of the mat-select.我假设这是因为您发送 #line (这是许多 mat-options 的模板变量)而不是发送 mat-select 的实际值。
I would try:我会尝试:
<form (ngSubmit)="addDetails(selectLine.value, selectMenu.value)"
[formGroup]="addMealForm">
<mat-card class="mb-5 float-right w-100">
<mat-form-field class="ml-5 mr-5 w-25">
<mat-label>Menu</mat-label>
<mat-select #selectMenu>
<mat-option #menu *ngFor="let menu of menus" [value]="menu.value">
{{menu.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="ml-5 mr-5 w-25">
<mat-label>line</mat-label>
<mat-select #selectLine>
<mat-option #line *ngFor="let line of lines" [value]="line.value">
{{line.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
<button type="submit" mat-raised-button color="accent">
<mat-icon>add</mat-icon>
Add Meal
</button>
</mat-card>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.