1

How can my use of $resource be converted to instead leverage the raw $http service? And what exactly does $resource do that $http does not?

return $resource(API_LINK+'/api/users/', {
   id: '@_id'
 },
 {
   changePassword: {
     method: 'PUT',
     params: {
       controller:'password'
     }
   },
   get: {
     method: 'GET',
     params: {
       id:'me'
     }
   })
scniro
  • 16,844
  • 8
  • 62
  • 106
Shrikant
  • 538
  • 5
  • 15
  • refer this question for difference between $http and $resources. http://stackoverflow.com/questions/13181406/angularjs-http-and-resource – Thangadurai Jan 12 '16 at 05:26
  • get : function () { $http.get(API_LINK + '/api/users/', {id:'me',controller:'password'}) .success(function (response) { console.log('response', response); //defer.resolve(response); }) .error(function (err) { console.log('err', err); // defer.reject(err); }); //return defer.promise; } – Shrikant Jan 12 '16 at 05:30
  • I did this and api works fine but giving one error, error is: – Shrikant Jan 12 '16 at 05:31
  • TypeError: Cannot read property 'hasOwnProperty' of undefined – Shrikant Jan 12 '16 at 05:31
  • Can you elaborate what you had done? Because above you passed two parameter in get method.. – Thangadurai Jan 12 '16 at 05:36

2 Answers2

2

$resource is simply an abstraction over $http with the idea that the API is convenient to use for RESTful endpoints. There is nothing $resource can do that can not be written using $http. A way to write the above in a factory leveraging $http may include...

// assumption that API_LINK is an injectable constant
.factory('MyService', function(API_LINK, $http) {

    function changePassword(params) {
        return $http.put(API_LINK +'/api/users/', params);
    }

    function get(id) {
        return $http.get(API_LINK +'/api/users?id=' + id);
    }

    return {
        changePassword: changePassword,
        get: get
    }
});

with the following usage...

.controller('ctrl', function($scope, MyService) {

    MyService.get('me').then(function(response) {
        // ...
    });

    MyService.changePassword({ controller: 'password' }).then(function(response) {
        // ...
    });
});

If you need to take total control of your factory functions with involved promise resolution, I would suggest checking out the AngularJS $q API.

scniro
  • 16,844
  • 8
  • 62
  • 106
0

we can also do with another way like this:

get: function() {
    var id = 'me';
    $http.get(API_LINK + '/api/users/' + id )
      .success(function(response) {
        defer.resolve(response);
      })
      .error(function(err) {
        console.log('err', err);
        defer.reject(err);
      });
    return defer.promise;
  },

  changePassword: function() {
    $http.put(API_LINK + '/api/users/', {controller:'password'} )
      .success(function(response) {
        defer.resolve(response);
      })
      .error(function(err) {
        console.log('err', err);
        defer.reject(err);
      });

    return defer.promise;
  }
Shrikant
  • 538
  • 5
  • 15