title: How to handle callback in angularjs?
body: <p>I have a registration mechanism in which rootscope variable is send via service. After success it updates the <code>$rootScope.success</code> field. But the angularjs services is callback dependent.The service update the rootscope.success but the function sequentially execute the code.</p>
  
  <p>How do i wait for service to complete its response and then further process?</p>
  
  <pre><code>.controller('RegisterAccountCtrl', function ($scope,$rootScope,registerUser,$location) {
  
      $rootScope.success = false;
      $scope.registration = $rootScope.registration;
  
  $scope.getEnterGeneratedCode = function(){
          $rootScope.registration = $scope.registration;
          registerUser.registerUser();
          if($rootScope.success){
             $location.path('/confirm');
          }
  }
  </code></pre>
  
  <p>And inside service</p>
  
  <pre><code>.service('registerUser',function($http,$rootScope,$ionicLoading){
      this.registerUser = function(){
          $ionicLoading.show();
          $http({
              method: 'POST',
              datatype:'json',
              data:{obj:$rootScope.registration},
              url: 'http://localhost/LoginService.asmx/CreateUser',
              contentType: "application/json; charset=utf-8",
              cache: false
          }).success(function (data, status, headers, config){
              if (status == '200') {
                  var obj = data;
                  $rootScope.success = true;
                  $ionicLoading.hide();
                  //alert(obj);
              }
          }).error(function (data, status, headers, config){
              $ionicLoading.hide();
          });
      };
  
   return this;
  })
  </code></pre>