title: Accessing returned scope method variables in another method in AngularJS
body: <p>I have an Angular app with a service and a controller:</p>
  
  <p><strong>service.js</strong></p>
  
  <pre><code>.factory('MyService', function ($http, $q) {
      var api_url = 'http://localhost/api/';
  
      var MyService = {
  
          list: function (items_url) {
              var defer = $q.defer();
              $http({method: 'GET', 
                  url: api_url + items_url}).
                  success(function (data, status, headers, config) {
                      defer.resolve(data);
                  }).error(function (data, status, headers, config) {
                      defer.reject(status);
                  });
              return defer.promise;
          },
          ...
     }
  });
  </code></pre>
  
  <p><strong>controller.js</strong></p>
  
  <pre><code>.controller("ItemsCtrl", function ($scope, MyService) {
  
      $scope.getItems = function () {
          MyService.list('items/').then(function(data) {
              $scope.items = data;
          });
      };
  
      $scope.addItems = function () {
          $scope.getItems();
  
          // why is this undefined ??!!!
          console.log($scope.items); 
      };
  </code></pre>
  
  <p>The issue is that I want to call the <code>$scope.getItems</code> method inside the <code>$scope.addItems</code> method. Do I perhaps need to use <code>$scope.apply()</code>, as the returned value is a promise?</p>
  
  <p>I think what I am displaying here is a general lack of understanding.</p>