title: How to catch a 401 (or other status error) in an angular service call?
body: <p>Using $http I can catch errors like 401 easily:</p>
  
  <pre><code>$http({method: 'GET', url: 'http://localhost/Blog/posts/index.json'}).
  success(function(data, status, headers, config) {
      $scope.posts = data;
  }).
  error(function(data, status, headers, config) {
      if(status == 401)
      {
          alert('not auth.');
      }
      $scope.posts = {};
  });
  </code></pre>
  
  <p>But how can I do something similar when using services instead. This is how my current service looks:</p>
  
  <pre><code>myModule.factory('Post', function($resource){
      return $resource('http://localhost/Blog/posts/index.json', {}, {
            index: {method:'GET', params:{}, isArray:true}
      });
  });
  </code></pre>
  
  <p>(Yes, I'm just learning angular).</p>
  
  <p><strong>SOLUTION (thanks to Nitish Kumar and all the contributors)</strong></p>
  
  <p>In the Post controller I was calling the service like this:</p>
  
  <pre><code>function PhoneListCtrl($scope, Post) {
      $scope.posts = Post.query();
  }
  //PhoneListCtrl.$inject = ['$scope', 'Post'];
  </code></pre>
  
  <p>As suggested by the selected answer, now I'm calling it like this and it works:</p>
  
  <pre><code>function PhoneListCtrl($scope, Post) {
      Post.query({},
      //When it works
      function(data){
          $scope.posts = data;
      },
      //When it fails
      function(error){
          alert(error.status);
    });
  }
  //PhoneListCtrl.$inject = ['$scope', 'Post'];
  </code></pre>