简体   繁体   中英

Can't set default value to ion-select ionic v4

I can't set default a value to ion-select ionic v4.

I have a simple <ion-select></ion-select> tag with only 2 options.

I'm binding with [(ngModel)] and I want to pre-select one of those two options when value is empty, such as "Employee" default selection.

Here's a extract of ion-select in user.component.html

<ion-select [(ngModel)]="user.type" id="type" name="type">
    <ion-option value="employee">Employee</ion-option>
    <ion-option value="vendor">Vendor</ion-option>
</ion-select>

I tried setting in ngOnInit() like this:

if(user.type == undefined  || user.type == "") user.type = "employee";

But I have no success so far. Here's how select turns out: 在此处输入图像描述

Why is it not understanding the instruction I gave before?

The ion-select has a Value tag, so just go ahead and set that:

<ion-select value="employee" [(ngModel)]="user.type" id="type" name="type">
    <ion-option value="employee">Employee</ion-option>
    <ion-option value="vendor">Vendor</ion-option>
</ion-select>

Also beware of caps, since you are setting the user type as "Employee" and not "employee" (which is used in the item-option)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM