Skip to content

Instantly share code, notes, and snippets.

@nicksergeant
Created January 29, 2014 02:52
Show Gist options
  • Save nicksergeant/8680922 to your computer and use it in GitHub Desktop.
Save nicksergeant/8680922 to your computer and use it in GitHub Desktop.
Angular services with $http.
// Controller.
function SearchView($scope, SavedSearches) {
// Get all of the user's saved searches.
SavedSearches.all().then(function(response) {
$scope.savedSearches = response.data;
});
// Create a new saved search.
$scope.create = function() {
var newSavedSearch = SavedSearches.create({
search: 'peanuts'
});
}
}
(function(angular, undefined){'use strict';
angular.module('something')
.factory('SavedSearches', function($http) {
return {
all: function() {
var promise = $http({
method: 'GET',
url: '/api/saved-searches/'
});
return promise;
},
create: function(savedSearch) {
var promise = $http({
method: 'POST',
url: '/api/saved-searches/',
data: savedSearch
});
return promise;
},
delete: function(savedSearch) {
var promise = $http({
method: 'DELETE',
url: '/api/saved-searches/' + savedSearch._id + '/'
});
return promise;
},
};
});
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment