IONIC 2 navbar left and right button not working

2019-07-27 09:50发布

Because of the title issue in ionic 2 for android. i have set some css and make centre of title.and i have put the left and right button in the nav bar. but when i apply the onclick functionality for that two button.its not working. even no console message too.

here is my code :

html :

<ion-header>
  <!-- use ion-toolbar for a normal toolbar and ion-navbar for navigation -->
  <ion-toolbar>
    <ion-buttons class="loginnavbtn" (click)="goback()" left>

    CANCEL
    <!-- left aligned content here -->
    </ion-buttons>

    <ion-title>
      LOGIN
    </ion-title>

    <ion-buttons class="loginnavbtn" (click)="loginbtntap()" right>
    SAVE
      <!-- left aligned content here -->
    </ion-buttons>
  </ion-toolbar>
</ion-header>
<ion-content>


   </ion-content>

My css :

ion-header {
  .button-md {
    box-shadow: none;
  }

  .toolbar-title {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row;
    flex-direction: row;
    -webkit-align-items: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 500;
  }
}

my js :

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



@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})

export class LoginPage {
  constructor(public navCtrl:NavController) {
  }

 public goback() {
    this.navCtrl.pop();
}
public loginbtntap() {
    this.navCtrl.pop();
}

}

My onclick is not working.what i ma doing wrong ?

Thanks !!

3条回答
冷血范
2楼-- · 2019-07-27 10:07

Here is the solution that works.

<ion-header>
 <ion-navbar>
  <ion-buttons *ngIf="user != null" left>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
  </ion-buttons>
  <ion-buttons  *ngIf="user == null" left>
    <button ion-button (click)="loginBtnClicked()">
      <ion-icon name="log-in"></ion-icon>
    </button>
  </ion-buttons>
<ion-title>Home</ion-title>
  <ion-buttons right>
    <button ion-button (click)="searchClicked()">
    <ion-icon name="search"></ion-icon>
  </button>
  </ion-buttons>

查看更多
女痞
3楼-- · 2019-07-27 10:20

Try to use ion-button

<button ion-button class="loginnavbtn" (click)="goback()" left>Name of button</button>
查看更多
唯我独甜
4楼-- · 2019-07-27 10:23

I faced the same issue in ionic3.
Please use button tag with ion-buttons and this solution works for me.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    <ion-header>
      <!-- use ion-toolbar for a normal toolbar and ion-navbar for navigation -->
      <ion-toolbar>
        <button ion-buttons class="loginnavbtn" (click)="goback()" left>

        CANCEL
        <!-- left aligned content here -->
        </button>

        <ion-title>
          LOGIN
        </ion-title>

        <button ion-buttons class="loginnavbtn" (click)="loginbtntap()" right>
        SAVE
          <!-- left aligned content here -->
        </button>
      </ion-toolbar>
    </ion-header>
    <ion-content>


       </ion-content>
查看更多
登录 后发表回答