I am making an API call from my AngularJS app using the $http
directive in a factory (I've tried both $http
and $resource
and I don't really care which I use as long as it returns the data), and I am getting either a $$state
object when I don't use the .then()
to unwrap the promise or an undefined
.
This is the code for the factory:
app.service('dataService', ['$http', function($http) {
return {
quizQuestions: function() {
return $http.get("http://localhost:59143/api/Questions")
.then(function(response) {
console.log(response);
console.log(response.data);
console.log(response.data[0]);
return response;
})
}
}
}])
When I call it from my controller, I do this:
app.controller('QuizController', ['$scope', '$http', 'dataService', function($scope, $http, dataService) {
dataService.quizQuestions().then(function(data) {
$scope.quizQuestions=data;
console.log($scope.quizQuestions);
})
}])
At this point, I'm not even trying to bind the data to the view and am just observing the console output. What I get with this configuration in the console is this:
{data: array(1), status: 200, heders: f, config: {...}, statusText: "OK"}
[{...}]
{Text: "...", *rest of object omitted for brevity but it is here*}
undefined
Can anyone tell me what I'm doing wrong?
As an aside, when I eliminate the factory all together and have the code in the controller as such:
app.controller('QuizController',['$scope', '$http', function($scope, $http) {
$http.get("http://localhost:59143/api/Questions").then(function(response) {
console.log(response);
console.log(response.data);
console.log(response.data[0]);
$scope.quizQuestions=response.data;
console.log($scope.quizQuestions);
console.log($scope.quizQuestions[0]);
})
}])
I get the console output of:
{data: array (etc)}
[{...}]
{text: (rest of obj)}
[{...}]
{text: (rest of obj)}
so it appears to be something in the factory?
I've looked at these other articles for some direction and the angular documentation but to no avail.