简体   繁体   English

无法将默认值设置为离子选择离子 v4

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

I can't set default a value to ion-select ionic v4.我无法将默认值设置为 ion-select ionic v4。

I have a simple <ion-select></ion-select> tag with only 2 options.我有一个简单的<ion-select></ion-select>标签,只有 2 个选项。

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.我正在与[(ngModel)]绑定,并且我想在值为空时预先选择这两个选项之一,例如"Employee"默认选择。

Here's a extract of ion-select in user.component.html这是user.component.htmlion-select的摘录

<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:我尝试像这样在ngOnInit()中设置:

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

But I have no success so far.但到目前为止我还没有成功。 Here's how select turns out: select 结果如下: 在此处输入图像描述

Why is it not understanding the instruction I gave before?为什么它不理解我之前给出的指令?

The ion-select has a Value tag, so just go ahead and set that:离子选择器有一个值标签,所以只需 go 并设置:

<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)还要注意上限,因为您将用户类型设置为“员工”而不是“员工”(在项目选项中使用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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