Last active
December 28, 2015 01:29
-
-
Save marbiano/7420787 to your computer and use it in GitHub Desktop.
Angular $http & $resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With $http: | |
myAppModule.factory('CreditCard', ['$http', function($http) { | |
var baseUrl = '/user/123/card'; | |
return { | |
get: function(cardId) { | |
return $http.get(baseUrl + '/' + cardId); | |
}, | |
save: function(card) { | |
var url = card.id ? baseUrl + '/' + card.id : baseUrl; | |
return $http.post(url, card); | |
}, | |
query: function() { | |
return $http.get(baseUrl); | |
}, | |
charge: function(card) { | |
return $http.post(baseUrl + '/' + card.id, card, {params: {charge: true}}); | |
} | |
}; | |
}]); | |
// With $resource: | |
myAppModule.factory('CreditCard', ['$resource', function($resource) { | |
return $resource('/user/:userId/card/:cardId', | |
{userId: 123, cardId: '@id'}, | |
{charge: {method:'POST', params:{charge:true}, isArray:false}); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment