Not able to fetch scope value in back button method in Ionic

On same template ionic is unable to get the value in ion-nav-bar directive.

Problem - Whatever the amount I filled in textbox <input type="text" placeholder="70" ng-model="getamt"> in below mentioned code, I am able to get the same value at Getting Amt here: {{getamt}} ie.if I type 56 I am able to get 56 typed like Getting Amt here: 56 BUT I am expecting the same to print at Not updating Amt {{getamt}} but in this case I am not getting any value.

I need to send this value to my previous page..so I am trying to do this stuff.

Let me know how to fix this weird issue with ionic.

<ion-view>
  <ion-nav-bar class="bar-stable">
    <ion-nav-back-button class="button-clear" ng-click="myGoBack(getamt)">
      <i class="icon ion-ios7-arrow-back"></i> Not updating Amt {{getamt}}
    </ion-nav-back-button>
  </ion-nav-bar>

  <ion-content class="has-subheader" padding-bottom="true">
    <div class="row">
      <div class="col col-30">
        <input type="text" placeholder="70" ng-model="getamt">
      </div>
      <div class="col col-30 item">
        Getting Amt here:  {{getamt}}
      </div>
    </div>
  </ion-content>
</ion-view>

EDIT -

My controller code -

.controller('mystate2Ctrl', function($scope, $stateParams, $localstorage, $rootScope, $ionicHistory) {
  console.log("----- mystate2Ctrl -----");

  $scope.myGoBack = function(val){
    console.log(val);
    $ionicHistory.goBack();
  };

  $rootScope.$on( "$ionicView.leave", function( scopes, states ) {
    if( states.stateName == "app.mystate1" ) {
      console.log("In Ionic View blah blah code here");
    } 
});

It seems like because, your Getting Amt here: {{getamt}} and Not updating Amt {{getamt}} in two different directives. So make sure they both can access mystate2Ctrl.

And try to define the variable in the controller itself.

.controller('mystate2Ctrl', function($scope, $stateParams, $localstorage, $rootScope, $ionicHistory) {

  $scope.getamt = 0;
  // your code 
})

HTH

The problem you're facing has to do with nested scopes and in particular with the way you use your "model".

The scope is not the model.

I would suggest you to watch this video from Miško Hevery where he talks about the problem you're having. At some point he says something like this:

Whenever you have ng-model there’s gotta be a dot in there somewhere. If you don’t have a dot, you’re doing it wrong.

Anyway, if you want to fix your problem you should define a model in your controller.

The solution is to create one:

.controller('mystate2Ctrl', function($rootScope, $scope, $state) {

  $scope.mymodel = { getamt: 0 };

});

Now you can reference your model in your views using the dot and your view should look something like this:

<ion-view view-title="my-app">

  <ion-nav-buttons side="right" class="button-clear" ng-click="myGoBack(mymodel.getamt)">
    <i class="icon ion-ios7-arrow-back"></i> Not updating Amt {{mymodel.getamt}}
  </ion-nav-buttons>

  <ion-content class="has-subheader" padding-bottom="true">
    <div class="row">
      <div class="col col-30">
        <input type="text" placeholder="70" ng-model="mymodel.getamt">
      </div>
      <div class="col col-30 item">
        Getting Amt here:  {{mymodel.getamt}}
      </div>
    </div>
  </ion-content>
</ion-view>

If you want to see how it works you can check my plunker.

Probably the best approach is the one John Papa suggests in his Angular guidelines.

If you read carefully the controllerAs View Syntax here he states why you should go for this approach:

Why?: It promotes the use of binding to a "dotted" object in the View (e.g. customer.name instead of name), which is more contextual, easier to read, and avoids any reference issues that may occur without "dotting".

Why?: Helps avoid using $parent calls in Views with nested controllers.

This is a second plunker with the controllerAs sample.