$http.get
function will provide you with a promise when called. Remember to return this promise from your factory and then handle it in your controller by using then
.
Within your factory
app.factory("bookService", ["$rootScope","$http",
function($rootScope,$http,$q) {
var getBooks = function(){
return $http.get('books.json'); // This will give you a promise
}
return {
getBooks: getBooks
};
}]);
And within your controller
app.controller("listController", ["$scope", "$location", "$routeParams", "bookService",
function($scope, $location, $routeParams, bookService) {
bookService.getBooks().then(function(data){ // Handle the promise using then()
// Success callback
$scope.books = data;
});
}
]);