Created
January 29, 2014 02:52
-
-
Save nicksergeant/8680922 to your computer and use it in GitHub Desktop.
Angular services with $http.
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
// 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' | |
}); | |
} | |
} |
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
(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