Align navbar back button on right side

2019-07-13 02:14发布

问题:

I'm developing an ionic app and I need to align the navbar back button on right side. The following is my code:

<ion-view title="Home" ng-controller="HomeCtrl" style="padding-top: 50px">
  <ion-nav-bar class="bar-balanced bar-dark">
    <ion-nav-back-button></ion-nav-back-button>
  </ion-nav-bar>
  <ion-content scroll="true">
    <ion-list>
      <ion-item ng-repeat="item in items" href="#/res/{{item}}">
        {{item.toUpperCase()}}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

And I've tried following code for aligning button using ion-nav-buttons:

<ion-view title="Home" ng-controller="HomeCtrl" style="padding-top: 50px">
  <ion-nav-bar class="bar-balanced bar-dark">
    <ion-nav-buttons side="right">
      <ion-nav-back-button></ion-nav-back-button>
    </ion-nav-buttons>
  </ion-nav-bar>
  <ion-content scroll="true">
    <ion-list>
      <ion-item ng-repeat="item in items" href="#/res/{{item}}">
        {{item.toUpperCase()}}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

But the above code throwing error like:

Error: [$compile:ctreq] Controller 'ionNavBar', required by directive 'ionNavBackButton', can't be found!
http://errors.angularjs.org/1.4.3/$compile/ctreq?p0=ionNavBar&p1=ionNavBackButton
    at http://localhost:8100/lib/ionic/js/ionic.bundle.js:13380:12
    at getControllers (http://localhost:8100/lib/ionic/js/ionic.bundle.js:21319:19)
    at nodeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:21445:33)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20853:13)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20857:13)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20857:13)
    at nodeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:21457:24)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20853:13)
    at compositeLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20857:13)
    at publicLinkFn (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20728:30) <ion-nav-bar class="bar-balanced bar-dark nav-bar-container" nav-bar-transition="ios">(anonymous function) @ ionic.bundle.js:25642
ionic.bundle.js:1173 'webkitMovementX' is deprecated. Please use 'movementX' instead.

回答1:

You can use this code:

<ion-nav-buttons side="right">
    <button class="button button-icon button-clear ion-arrow-left-c" ng-click="goBack()">back
    </button>
</ion-nav-buttons>

and in your controller(s):

$scope.goBack = function() {
  $ionicHistory.goBack(-1);
}

P.S.: in this case no <ion-nav-back-button></ion-nav-back-button> is used

Here is an example: http://codepen.io/beaver71/pen/NNWZBp