首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按住ion-select选择不起作用的默认值

按住ion-select选择不起作用的默认值
EN

Stack Overflow用户
提问于 2017-07-31 23:31:32
回答 6查看 11.7K关注 0票数 1

我有离子选择,有很多选项,当视图准备好根据CurrentNumber选择一个默认值时,我也有。我有这个代码:

代码语言:javascript
复制
<ion-select formControlName="Level">
          <ion-option [value]="level.id" *ngFor="let level of levels" [attr.selected]="(level.levelNumber == currentLevel)? true : null">
             {{level.name}}
          </ion-option>
</ion-select>

this.currentLevel = 1;

数据来自服务器,如下所示:

代码语言:javascript
复制
data = [
 {
  levelNumber : 1,
  name:'abcd'
 },
 {
levelNumber:2,
name:'efg'
 }
]
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2017-07-31 23:54:42

当数据准备就绪时,您可以在控件中设置默认选项,而不是selected属性:

代码语言:javascript
复制
// When the data is ready
this.yourForm.get('Level').setValue(data.id);

这会将id等于data.id的选项设置为默认值。

票数 7
EN

Stack Overflow用户

发布于 2017-10-25 18:16:27

我找到了一种使用占位符的方法

代码语言:javascript
复制
<ion-select id="genderInput" [formControl]="gender" [placeholder]="gender.value.toUpperCase() | translate">
   <ion-option value="female">{{ 'FEMALE' | translate }}</ion-option>
   <ion-option value="male">{{ 'MALE' | translate }}</ion-option>
</ion-select>

我还需要添加以下样式来覆盖占位符的默认颜色

代码语言:javascript
复制
ion-select {
   div.select-text.select-placeholder {
      color: get-color(black) !important;
   }
}

希望它能有所帮助:)

票数 5
EN

Stack Overflow用户

发布于 2017-08-01 00:00:18

然后是这个:直接来自here的示例

代码语言:javascript
复制
 * ### Object Value References
 *
 * When using objects for select values, it is possible for the identities of these objects to
 * change if they are coming from a server or database, while the selected value's identity
 * remains the same. For example, this can occur when an existing record with the desired object value
 * is loaded into the select, but the newly retrieved select options now have different identities. This will
 * result in the select appearing to have no value at all, even though the original selection in still intact.
 *
 * Using the `compareWith` `Input` is the solution to this problem
 *
代码语言:javascript
复制
 <ion-item>
   <ion-label>Employee</ion-label>
   <ion-select [(ngModel)]="employee" [compareWith]="compareFn">
     <ion-option *ngFor="let employee of employees" [value]="employee">{{employee.name}}</ion-option>
   </ion-select>
 </ion-item>
代码语言:javascript
复制
 compareFn(e1: Employee, e2: Employee): boolean {
   return e1 && e2 ? e1.id === e2.id : e1 === e2;
 }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45419910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档