News for hackers. Stuff that matters.
Re:Diving into AngularJS by squidsoup on Sunday April 21, 2013 @04:58PM (#5586387)
Personally I found using $http less restrictive - there seem to be some edge cases where $resource behaves oddly. Ideally what you should be doing with REST calls is pushing them out to Angular Services (http://docs.angularjs.org/guide/dev_guide.services.creating_...) rather than making them directly in your controller.

Here's a simple example:

  appServices.factory('Thing', function($http) {

    var Thing = function(data) {
      angular.extend(this, data);
    }

    Thing.all = function() {
      return $http.get('/things).then(function(response) {
        return new Thing(response.data);
      });
    }

    Thing.get = function(id) {
      return $http.get('/things/' + id).then(function(response) {
        return new Thing(response.data);
      });
    }

    Thing.delete = function(id) {
      return $http.delete('/things/' + id).then(function(response) {
        return new Thing(response.data);
      });
    }

    Thing.create = function(data) {
      return $http.post('/things', data).then(function(response) {
        return new Thing(response.data);
      });
    }

    Thing.update = function(data, id) {
      return $http.put('/things/' + id, data).then(function(response) {
        return new Thing(response.data);
      });
    }

    return Thing;
  });
In your controller, inject the service:

  function thingCtrl($scope, Thing) {
    Thing.all().then(function(data) {
      $scope.things = data;
    });
  }
  thingCtrl.$inject = ['$scope', 'Thing'];
Re:Diving into AngularJS by grncdr on Sunday April 21, 2013 @10:04PM (#5587366)
Another nice feature of angular is that templates are promise-aware. So the following controller is equivalent for most purposes:

    function thingCtrl($scope, Thing) {
      $scope.things = Thing.all()
    }
    thingCtrl.$inject = ['$scope', 'Thing'];