Reputation: 3008
I guess the $http.get
part is not working .
Here is my code :
.controller('home', function($scope, request){
$scope.list = request.get(baseUrl);
})
.factory('request', function($http){
var get = function(url){
$http.get(url).success(function(data){
console.log(data);
return data; //returns nothing
});
}
return {
get : get
}
});
Upvotes: 1
Views: 691
Reputation: 2241
Try this.
.factory('request', function($http){
var get = function(url){
return $http.get(url).success(function(data){
return data;
});
}
return {
get : get
}
});
Update your controller.
request.get(url).then(function (data) {
$scope.data = data;
}, function (response) {
// handle error
});
http://plnkr.co/edit/eWXckgiwPNCGMlP0LQyk?p=preview
Upvotes: 3
Reputation: 2704
Generally speaking, your function doesn't return anything at all - if I were to call request.get(), it would return undefined. You want something like
.controller('home', function($scope, request){
request.get(baseUrl).then(function(data){ // I prefer then and catch semantics
$scope.list = data; // as they're standard promises form
})['catch'](function (err){console.log(err)});
})
.factory('request', function($http){
var get = function(url){
return $http.get(url).then(function(data){
console.log(data);
return data;
});
}
return {
get : get
}
});
Although this is unnecessary obfuscation for a simple case (i'd just put the $http.get().then().catch()
stuff in the controller instead of a factory.
Upvotes: 1