Search code examples
htmlangularjsvariablesionic-frameworkngcordova

How to pass data between pages in Ionic app


I have had a look at all the examples around and tried to integrate with my Ionic project and failed. Here is what I have so far.

I am using a project created using creator.ionic.com

I have an html page that im loading a JSON file into

(agentSchedule.html)

<ion-view style="" title="Agent Schedule">
 <ion-content class="has-header" overflow-scroll="true" padding="true">
  <ion-refresher on-refresh="doRefresh()">
    </ion-refresher>
    <div ng-controller="agentScheduleCtrl">
      <ion-list style="">
          <ion-item  class="item-icon-right item item-text-wrap item-thumbnail-left" ng-repeat="user in users">
            <img ng-src="{{ user.image }}">
          <h2>{{ user.fname }} {{ user.lname }}</h2>
          <h4>Role : {{ user.jobtitle }}</h4>
          <h4>Username : {{ user.username }}</h4><i class="button-icon icon ion-ios-arrow-forward"></i>
          </ion-item>
      </ion-list>
  </div>
</ion-content>

Here is the controller and routes for that page

.controller('agentScheduleCtrl', function($scope, $http, $timeout) {
  var url = "http://www.otago.ac.nz/itssd-schedule/mobileappdevice/services/getUsers.php";
  var url = "users.json";
  $http.get(url).success( function(response) {
      $scope.users = response;
});

.state('menu.agentSchedule', {
  url: '/page4',
  views: {
  'side-menu21': {
    templateUrl: 'templates/agentSchedule.html',
    controller: 'agentScheduleCtrl'
    }
  }
})

I am another page that I want to pass the username to, and then use on any page that I go from there

(specificAgentDetails.html)

<ion-view style="" view-title="Agent Specific Schedule">
<ion-content class="has-header" overflow-scroll="true" padding="true">

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

Here is the controller and routes for that page

.controller('specificAgentDetailsCtrl', function($scope) {

})

.state('menu.specificAgentDetailsCtrl', {
url: '/page9',
views: {
  'side-menu21': {
    templateUrl: 'templates/specificAgentDetailsCtrl.html',
    controller: 'specificAgentDetailsCtrl'
  }
}
})

How to Pass the user.username JSON variable to the next page as a global variable ??

Any help would be awesome - im going around in circles


Solution

  • So what @shershen said is true most ionic apps are set up as SPA's, what you're going to do isn't a "ionic" thing it's more of a AngularJS thing. You're going to want to setup a Service to hold your User info and then share it with your other controllers.

    1. Create a Factory to share the data.
    2. Inject the factory into the first controller and set it.
    3. Inject the factory into the second controller and retrieve it.

    Share data between AngularJS controllers

    If you're familiar with get/set in OOP you should have no problem.

    .factory('Data', function () {
    var user = {};
    return {
        getUser: function () {
            return user;
        },
        setUser: function (userparameter) {
            user = userparameter;
        }
    };
    })
    .controller('agentScheduleCtrl', function($scope, $http, $timeout, Data) {
     var url = "http://www.otago.ac.nz/itssdschedule/mobileappdevice/services/getUsers.php";
     var url = "users.json";
    $http.get(url).success( function(response) {
      Data.setUser(response);
      $scope.users = response;
    });
    

    Then in your second controller:

    .controller('specificAgentDetailsCtrl', function($scope, Data) {
       $scope.user = Data.getUser();
    })
    

    Make sure the syntax between the view and controllers are correct. I'm using user you're using users, setup breakpoints in your controllers to see what's going on there.