Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-click does not work in ionic header button

I am trying to add a button on the right side of the header bar, the button is showing, but ng-click does not get fired, if I move the button inside ion-content, it starts working, just not working in header.

html :

<ion-view>
    <ion-nav-bar class="bar-stable nav-title-slide-ios7" >
      <ion-nav-back-button class="button-icon icon  ion-ios7-arrow-back">
      </ion-nav-back-button>
    </ion-nav-bar>
    <ion-header-bar>
        <h1 class="title">Trips</h1>
        <div class="buttons pull-right"><button class="button button-icon icon ion-plus" ng-click="createTrip()"></button></div>
    </ion-header-bar>

    <ion-content>

    </ion-content>
</ion-view>

js:

.controller('TripCtrl', function($scope, $location, $localStorage){
    console.log('inside TripCtrl');

    $scope.$storage = $localStorage;

    var random1 = {
        name : 'random name 1',
        text : 'random text 1'
    }

    var random2 = {
        name : 'random name 2',
        text : 'random text 2'
    }

    $scope.trips = [];
    $scope.trips.push(random1);
    $scope.trips.push(random2);


    $scope.createTrip = function(){
        console.log('clicked create new');
        $location.path('/createTrip');
    }
})
like image 607
Jie Avatar asked Feb 09 '15 03:02

Jie


1 Answers

Please make sure createTrip is defined inside app's root controller and also note ion-header-bar doesn't listen for click event by default. if you want to bind click event inside ion-header-bar you must have to wrap the target element inside button block of ion-header-bar.

<ion-header-bar align-title="left" class="bar-positive">
  <div class="buttons">
    <button class="button" ng-click="doSomething()">Left Button</button>
  </div>
  <h1 class="title">Title!</h1>
  <div class="buttons">
    <button class="button">Right Button</button>
  </div>
</ion-header-bar>

http://ionicframework.com/docs/api/directive/ionHeaderBar/

like image 53
Sandeep vashisth Avatar answered Oct 01 '22 14:10

Sandeep vashisth