Reputation: 155
i have service which makes an $HTTP.GET request, at first i tried to access the object $$state but the value inside was undefined, so i went and red about promises and asynchronous calls here: http://blog.ninja-squad.com/2015/05/28/angularjs-promises/
after i red everything i tried again as guided but still it doesn't work for me :-(, please advise, i want to access the values object.
service:
function ConnectMe($http) {
function OnAction() {
this.create = function (search, key) {
return $http.get("https://www.tastekid.com/api/similar?q=" + search + "&verbose=1&k=" + key).then(function (response) {
return response.data;
});
};
}
var connection = new OnAction();
return function () {
return connection;
};
}
controller:
function TasteConroller($scope, ConnectMe) {
$scope.apiKey = "https://www.tastekid.com/api/similar?q=" + $scope.UserPick + "&verbose=1&k=254357-MatureAm-A31YEAIX";
$scope.UserPick = "red+hot+chili+peppers";
$scope.Key = "254357-MatureAm-A31YEAIX";
var srv = ConnectMe();
srv.create($scope.UserPick, $scope.Key).then(function(data) {
$scope.results = data;
}), function() {
$scope.error = 'unable to get the ponies';
};
console.log($scope.results);
Upvotes: 1
Views: 57
Reputation: 6813
Your service code looks a little suspect. I normally create a service like this;
function ServiceFunc( $http, $q ){
return {
OnAction: function( search, key ){
return $http.get(...)
.then( function( rsp ){ return rsp.data.foo })
.catch( function( err ){ $q.reject( err )})
}
}
I think you may have had a deeper level of functions whereby you weren't actually calling it.
Upvotes: 1