admin管理员组文章数量:1390410
I'm working with Angular material and using Mat-Select. When the user makes a selection I want to grab a user which is actually an object with a Id and description property.
<mat-form-field>
<mat-select placeholder="Select User Type" (selectionChange)="selectUserType(user)">
<mat-option *ngFor="let user of userTypeList" [value]="user.description">
{{ user.description }}
</mat-option>
</mat-select>
</mat-form-field>
When the user makes a selection I am receiving undefined here:
public selectUserType(userType: OxygenUserType) {
console.log('selected');
this.selectedUserType = userType;
console.log(this.selectedUserType);
}
I've also tried (selectionChange)="selectUserType($event.value)"
but this doesn't produce an object. I need the user selection to be an object that looks like this:
{id:1, description:Agent},
{id:2, description:Dealer}
This object is based on this interface
export interface OxygenUserType {
id: number;
description: string;
}
I'm working with Angular material and using Mat-Select. When the user makes a selection I want to grab a user which is actually an object with a Id and description property.
<mat-form-field>
<mat-select placeholder="Select User Type" (selectionChange)="selectUserType(user)">
<mat-option *ngFor="let user of userTypeList" [value]="user.description">
{{ user.description }}
</mat-option>
</mat-select>
</mat-form-field>
When the user makes a selection I am receiving undefined here:
public selectUserType(userType: OxygenUserType) {
console.log('selected');
this.selectedUserType = userType;
console.log(this.selectedUserType);
}
I've also tried (selectionChange)="selectUserType($event.value)"
but this doesn't produce an object. I need the user selection to be an object that looks like this:
{id:1, description:Agent},
{id:2, description:Dealer}
This object is based on this interface
export interface OxygenUserType {
id: number;
description: string;
}
Share
Improve this question
edited Jun 5, 2018 at 6:25
Anshuman Jaiswal
5,4621 gold badge31 silver badges46 bronze badges
asked Jun 5, 2018 at 5:55
LDBLDB
6114 gold badges14 silver badges32 bronze badges
5
-
Currently you are binding
user.description
tomat-option
, so when you select a option,selectionChange
's$event.value
will beuser.description
. If you want the entire object, just bind object itself tomat-option
. – Pengyy Commented Jun 5, 2018 at 6:09 - Thx. I tried this and it works. – LDB Commented Jun 5, 2018 at 13:32
- Well I'm encountering another issue. Once I save the selected user from the dropdown, the selection no longer persists once the view is changed. – LDB Commented Jun 5, 2018 at 14:16
-
That is because you don't give
mat-select
a init value. You should bind it a init value via[(ngModel)]
. – Pengyy Commented Jun 6, 2018 at 0:38 - Thx for that. I was able to resolve by adding [( ngModel )]. – LDB Commented Jun 6, 2018 at 13:26
2 Answers
Reset to default 6You can use [(value)]
to set selected user. In option's value assign user object rather than only description as:
<mat-select placeholder="Select User Type" [(value)]="selectedUserType" (selectionChange)="onUserTypeChange()">
<mat-option *ngFor="let user of userTypeList" [value]="user">
{{ user.description }}
</mat-option>
</mat-select>
public onUserTypeChange() {
console.log(this.selectedUserType);
}
I think your way doesn't work because you declare your user in mat-option, so mat-select doesn't know about this.
I would solve this by using a model so you don't need a function to store your value. If you want to still do some other stuff when the model changes you can call your value from the model in your change function.
so your html would be:
<mat-form-field>
<mat-select placeholder="Select User Type" [(ngModel)]="selectedUserType" (change)="selectUserType()">
<mat-option *ngFor="let user of userTypeList" [value]="user">
{{ user.description }}
</mat-option>
</mat-select>
</mat-form-field>
and in your method:
public selectUserType() {
console.log('selected');
console.log(this.selectedUserType);
// do some stuff with your value.
}
本文标签: javascriptMatSelect returns undefinedStack Overflow
版权声明:本文标题:javascript - Mat-Select returns undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664575a2618448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论