title: Angularjs: store data into variables from one factory call to use in an another factory call
body: <p>I am totally new to angularjs and ionic. I try to store 4 values which i receive from a factory call into 4 variables which i need to use as parameters for my $http get call in another function:</p>
  
  <p>here are the 4 variables defined at the beginning of my controller:</p>
  
  <pre><code>var firstId;
  var firstTimestamp;
  var lastId;
  var lastTimestamp; 
  </code></pre>
  
  <p>I receive the data from the following call to my factory and assign the data to the variables:</p>
  
  <pre><code>ContentFactory.getAlbums().then(function(albums){
  
     $scope.albums = albums;
  
     firstId = albums.first_id;
     firstTimestamp = albums.first_timestamp;
     lastId = albums.last_id;
     lastTimestamp = albums.last_timestamp;
  });
  </code></pre>
  
  <p>I try then to use these variables as parameters for another call to my factory (loadOlderAlbums is called in the view):</p>
  
  <pre><code>$scope.loadOlderAlbums = function(lastId, lastTimestamp) {
   ContentFactory.getOlderAlbums(lastId, lastTimestamp).then(function(albums){
      $scope.albums = $scope.albums.concat(albums);
      $scope.$broadcast('scroll.infiniteScrollComplete');
   });
  };
  </code></pre>
  
  <p>The variables lastId &amp; lastTimestamp are undefined when checking my factory:</p>
  
  <pre><code>getOlderAlbums : function(lastId, lastTimestamp){
          console.log(lastId); // lastId is undefined
          return $http.get('http://api.domain.xxx/albums/city/1? &amp;direction=before&amp;id=' + lastId + '&amp;time=' + lastTimestamp + '&amp;api_key=******&amp;lang=en').then(function(response) {
                  albums = response.data;
                  return albums;
          });
      }
  </code></pre>
  
  <p>in the console i see the following error:</p>
  
  <pre><code>GET http://api.domain.xxx/albums/city/1?&amp;direction=before&amp;id=undefined&amp;time=undefined&amp;api_key=***&amp;lang=en 500 (Internal Server Error)
  </code></pre>
  
  <p>because id &amp; time are undefined. </p>
  
  <p>here is the complete controller:</p>
  
  <pre><code>.controller('PicturesCtrl', function($scope, $ionicLoading,  ContentFactory) {
  
  $scope.albums = [];
  
  var firstId;
  var firstTimestamp;
  var lastId;
  var lastTimestamp; 
  
  ContentFactory.getAlbums().then(function(albums){
    $scope.albums = albums;
    firstId = albums.first_id;
    firstTimestamp = albums.first_timestamp;
    lastId = albums.last_id;
    lastTimestamp = albums.last_timestamp;
  
    // console.log(firstId);  -&gt; values are correctly stored
    // console.log(firstTimestamp); -&gt; values are correctly stored
    // console.log(lastId); -&gt; values are correctly stored
    // console.log(lastTimestamp); -&gt; values are correctly stored
  
   });
  
  
  $scope.loadOlderAlbums = function(lastId, lastTimestamp) {
   ContentFactory.getOlderAlbums(lastId, lastTimestamp).then(function(albums){
      $scope.albums = $scope.albums.concat(albums);
      $scope.$broadcast('scroll.infiniteScrollComplete');
   });
  };
  
  })
  </code></pre>
  
  <p>and the factory:</p>
  
  <pre><code>.factory('ContentFactory', function($http) {
  
  var albums = [];
  
  return {
  
  
  
      getAlbums : function(){
          return $http.get('http://api.domain.xxx/albums/city/1?lang=en&amp;api_key=***').then(function(response) {
                  albums = response.data;
                  return albums;
          });
      },
  
  
      getOlderAlbums : function(lastId, lastTimestamp){
          console.log(lastId); // lastId is undefined
          return $http.get('http://api.domain.xxx/albums/city/1?&amp;direction=before&amp;id=' + lastId + '&amp;time=' + lastTimestamp + '&amp;api_key=***&amp;lang=en').then(function(response) {
                  albums = response.data;
                  return albums;
          });
      }
     }
  })
  </code></pre>
  
  <p>Any help to figure out why the value of the variables are not passed to the second factory call are really appreciated. Its probably a logical issue as i'm a total newbie ;)</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Please find below a sample response which I received from my first $http GET call in Content.Factory.getAlbums</p>
  
  <pre><code>{
  "city_id": 1,
  "total_pages": 199,
  "total_results": 3977,
  "first_id": 15448,
  "first_timestamp": 1437230820,
  "results": [
      {
          "album_id": "15448",
          "title": "MAD pres. Splash The Madness Party at Aftermoon",
          "description": "The MAD crew is back and returned to Aftermoon for a night of Melbourne Bounce, Hip Hop, Big Room, Electro- and Progressive House. At the decks were Dam-Sib-Dao, Beatender, Madz Mellow &amp; Odyszey, supported by Mc Ben10.",
          "photographer_id": "8",
          "photographer_name": "ploy",
          "image_count": "88",
          "cover_image": "5dd26da7f37da641d775568d2e98ae44.jpg",
          "date": "2015-07-18 21:47:00",
          "location_id": "705",
          "location_name": "Aftermoon"
      },
      {
          "album_id": "15446",
          "title": "Superhero Charity Party at KU DÉ TA Bangkok",
          "description": "KU DÉ TA Bangkok and the charity organisation \"Habitat for Humanity Thailand\" joined forces for the SUPERHERO “CHARITY PARTY\". Habitat for Humanity Thailand improves the quality of Thai people’s lives through building homes and transforming communities. Entry is Free, but there guests are invited to do a FREE-WILL DONATION of which 100% will go to Habitat for Humanity Thailand to build houses under the program “Make Her Day” in Pathumthani.",
          "photographer_id": "15",
          "photographer_name": "win",
          "image_count": "176",
          "cover_image": "742a0065d046418041175ff8005d7174.jpg",
          "date": "2015-07-18 18:46:00",
          "location_id": "809",
          "location_name": "KU DÉ TA"
      }
  ],
  "last_id": 15427,
  "last_timestamp": 1437062520
  }
  </code></pre>
  
  <p>The $scope.loadOlderAlbums is called in my view (Ionic infinite scroll)</p>
  
  <pre><code>&lt;ion-infinite-scroll
    on-infinite="loadOlderAlbums()"
    distance="1%"&gt;
  &lt;/ion-infinite-scroll&gt;
  </code></pre>