title: Angularjs + two http request using $q
body: <p>I have two http requests.
  1. post method
  2. get method.</p>
  
  <p>I need to get data from two http request.</p>
  
  <p>I added my code. But i think this is not correct approach. Please suggest me.</p>
  
  <p>Question 1:</p>
  
  <p>After two call to synchronous.
  Controller</p>
  
  <pre><code>(function() {
      'use strict';
      angular.module('myApp').controller('loginController', loginController);
      loginController.$inject = ['$auth', '$http', '$location', '$scope', '$window', '$rootScope', 'commonService', '$q', '$localStorage'];
      // inject an auth service here!
      function loginController($auth, $http, $location, $scope, $window, $rootScope, commonService, $q, $localStorage) {
          commonService.getHostDetails().then(function(data) {
              commonService.login().then(function(data) {
  
              }).catch(function(data) {
                  alert('Sorry ! test function faild');
              });
          }).catch(function(data) {
              alert('Sorry ! login function faild');
          });
      };
  
  })();
  </code></pre>
  
  <p>Service Code:</p>
  
  <pre><code>angular.module('myApp').factory('commonService', ['$q', '$timeout', '$http',
  function($q, $timeout, $http, commonService) {
  
      return ( {
          login : login,
          test : test,
      });
  
      function login() {
          // create a new instance of deferred
          var deferred = $q.defer();
          $http.get('host.json')
          // handle success
          .success(function(data, status) {
              deferred.resolve(data);
          })
          // handle error
          .error(function(data) {
              deferred.reject(data);
  
          });
          // return promise object
          return deferred.promise;
  
      }
  
      function test(formData) {
          // create a new instance of deferred
          var deferred = $q.defer();
          console.log("in service");
          console.log(formData);
          $http.post('/api/test', formData, {
              headers : {
                  'Content-Type' : 'application/json'
              }
          })
          // handle success
          .success(function(data) {
              deferred.resolve(data);
          })
          // handle error
          .error(function(data) {
              deferred.reject(data);
  
          });
  
          // return promise object
          return deferred.promise;
      }
  
  }]);
  </code></pre>
  
  <p>Question -2.</p>
  
  <p>Also please suggestme two http request depends on two api.
  First http request data need to parse into second api.</p>