2016-11-30 42 views

回答

4

TS

export class Page1{ 
    public toggled: boolean = false; 

    constructor() { 
     this.toggled = false; 
    } 

    public toggle(): void { 
     this.toggled = this.toggled ? false : true; 
    } 
} 

HTML

<div> 
    <ion-icon *ngIf="!toggled" (click)="toggle()" name="search"></ion-icon> 
    <ion-searchbar 
     *ngIf="toggled" 
     [(ngModel)]="someValue" 
     (ionInput)="searchThis($event)" 
     (ionCancel)="cancelSearch($event)" 
     (ionClear) = "cancelSearch($event)" 
     [showCancelButton]="true"> 
    </ion-searchbar> 
</div> 

在你cancelSearch()你可以叫this.toggle()再次顯示圖標。

+0

好的,謝謝它的工作原理,但有一個小錯誤,當它運行在ios標題是與搜索欄膠體。 –

+0

要調試,我們需要您的代碼 – Ivaro18

+1

HTML' <離子導航欄顏色= 「橙色」> <鍵離子鍵menuToggle> <離子圖標名稱= 「菜單」> MyOrder <離子按鈕結束> <鈕離子按鈕圖標向左* ngIf = 「!切換」(點擊)= 「toggleSearch()」> <離子圖標名稱= 「搜索」> ' –

2

我覺得@ Ivaro18的答案是完美的。

我只是想補充一下我的例子,根據他的回答建立。

home.ts

import { Component } from '@angular/core'; 
import { NavController, NavParams } from 'ionic-angular'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 

    toggled: boolean; 
    searchTerm: String = ''; 
    items: string[]; 

    constructor(public navCtrl: NavController, public navParams: NavParams) { 
     this.toggled = false; 
     this.initializeItems();   
    } 

    ionViewDidLoad() { 
     console.log('ionViewDidLoad HomePage'); 
    } 

    toggleSearch() { 
     this.toggled = this.toggled ? false : true; 
    } 

    initializeItems() { 
     this.items = ['Amsterdam','Bogota','Mumbai','San José','Salvador']; 
    }  

    triggerInput(ev: any) { 
     // Reset items back to all of the items 
     this.initializeItems(); 
     // set val to the value of the searchbar 
     let val = ev.target.value; 
     // if the value is an empty string don't filter the items 
     if (val && val.trim() != '') { 
      this.items = this.items.filter((item) => { 
      return (item.toLowerCase().indexOf(val.toLowerCase()) > -1); 
      }) 
     } 
    } 
} 

home.html的

<ion-header> 
    <ion-navbar color="primary"> 
    <button *ngIf="!toggled" ion-button icon-only menuToggle><ion-icon name="menu"></ion-icon></button> 
    <!-- Title --> 
    <ion-title *ngIf="!toggled">In&iacute;cio</ion-title> 
    <!-- Search Bar --> 
    <ion-searchbar *ngIf="toggled" [(ngModel)]="searchTerm" [showCancelButton]="true" (ionCancel)="toggleSearch()" (ionInput)="triggerInput($event)"></ion-searchbar> 
    <!-- Search Icon -->  
    <ion-buttons end *ngIf="!toggled"> 
     <button ion-button icon-only (click)="toggleSearch()"><ion-icon name="search"></ion-icon></button> 
    </ion-buttons> 
    </ion-navbar> 
</ion-header> 

<ion-content padding> 

<ion-list> 
    <ion-item *ngFor="let item of items"> 
    {{ item }} 
    </ion-item> 
</ion-list> 
</ion-content> 

更多信息: 欲瞭解更多信息,請查看API docs

+0

如何在搜索欄上添加焦點並在用戶點擊搜索圖標時打開鍵盤? –

相關問題