【问题标题】:How to pop out ion-select using different button如何使用不同的按钮弹出离子选择
【发布时间】:2018-07-17 07:05:55
【问题描述】:

如何使用不同的按钮弹出离子选择?

<ion-select [(ngModel)]="choices" multiple="true">
        <ion-option>Appliances</ion-option>
        <ion-option>Automobile</ion-option>
        <ion-option>Cellphones</ion-option>
        <ion-option>Clothing</ion-option>
        <ion-option>Computers</ion-option>
        <ion-option>Electronics</ion-option>
        <ion-option>Toys</ion-option>
</ion-select>

【问题讨论】:

    标签: ionic-framework ionic3


    【解决方案1】:

    你可以ViewChildionic-angular

    html

    <ion-select [(ngModel)]="choices" multiple="true" #mySelect>
         <ion-option>Appliances</ion-option>
         <ion-option>Automobile</ion-option>
         <ion-option>Cellphones</ion-option>
         <ion-option>Clothing</ion-option>
         <ion-option>Computers</ion-option>
         <ion-option>Electronics</ion-option>
         <ion-option>Toys</ion-option>
    </ion-select>
    
    <button ion-button (click)="openSelect()">Open</button>
    <button ion-button (click)="closeSelect()">Close</button>
    

    ts

    import { Component, ViewChild } from '@angular/core';
    import { NavController,Content, Select } from 'ionic-angular';
    import { Events } from 'ionic-angular';
    
    @Component({
        selector: 'page-home',
        templateUrl: 'home.html'
    })
    export class HomePage 
    {    
        @ViewChild('mySelect') selectRef: Select;
        constructor(public navCtrl: NavController,public events: Events) 
        {}
    
        openSelect()
        {
            this.selectRef.open();
        }
    
        closeSelect()
        {
            this.selectRef.close();
        }
    }
    

    【讨论】:

    • 迄今为止我发现的最清晰、最简单的例子。它使我的代码工作。谢谢
    • 很高兴为您提供帮助!
    • 谢谢,但是当我们在页面中为其他人提供多个选择元素时,您的意见是什么?
    • @PareshGami 是的,但我的第二个元素似乎在初始化后加载数据。?怎么样?你能帮我或发送一篇文章或 StackOverflow 链接吗? (ionSelect) 无效!如何在数据启动后以编程方式添加事件侦听器?在这个例子中stackoverflow.com/q/41237061/308578
    • @Paresh Gami 你能告诉我 ionic4 的相同解决方案吗?
    【解决方案2】:

    感谢@PareshGami

    但在 ionic 4 中,如果您只想在单击 button 时显示列表并隐藏 select

    1.导入IonSelect

    import { Component, ViewChild } from '@angular/core';
    import { Platform, Events, IonSelect } from '@ionic/angular';
    

    2.在类内部,添加对select的引用并将showList设置为true以隐藏select。还要添加setCountry() 以检索所选国家/地区。

    @ViewChild('countryList') selectRef: IonSelect;
    showList = true;
    
    setCountry() {
        console.log('New country');
    }
    

    3.在 HTML 中,添加 select 具有隐藏属性的元素

    <ion-select placeholder="Country" #countryList [hidden]='showList' (ionChange)='setCountry()'>
        <ion-select-option value="1">Egypt</ion-select-option>
        <ion-select-option value="2">Kuwait</ion-select-option>
        <ion-select-option value="3">UAE</ion-select-option>
        <ion-select-option value="4">Qatar</ion-select-option>
        <ion-select-option value="5">Bahrain</ion-select-option>
        <ion-select-option value="6">Saudi Arabia</ion-select-option>
    </ion-select>
    
    <ion-label (click)='displayCountry()'>Change</ion-label>
    

    所以select 元素是不可见的,单击Change 将显示要选择的国家列表。

    【讨论】:

    • 您知道如何在代码上方设置界面吗?我的意思是设置 interface=popover。
    【解决方案3】:

    下面的代码有效。

    在模板中 (HTML)

    <ion-button (click)="openSelectList()"></ion-button>
     <ion-select #countryList (ionChange)='setCountry($event)' hidden="true">
        <ion-select-option value="1">Egypt</ion-select-option>
        <ion-select-option value="2">Kuwait</ion-select-option>
    </ion-select>
    

    在.ts中

    @ViewChild('countryList') selectRef: IonSelect;
       ...
    
       openSelectionList() {
         setTimeout(()=>{
           this.selectRef.open();
         }, 2);
       }
    
       setCountry(event) { console.log('Selected country is ', event.detail.value) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2020-02-03
      • 2019-04-14
      • 2020-01-31
      相关资源
      最近更新 更多