我有一个发出$ HTTP.GET请求的服务,起初我试图访问对象$$状态,但是里面的值是未定义的,所以我在这里红了一下promises和异步调用: http://blog.ninja-squad.com/2015/05/28/angularjs-promises/
我红了所有的东西后我再次尝试按照指导,但它仍然不适合我:-(,请指教,我想访问值对象。
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);
答案 0 :(得分:1)
您的服务代码看起来有点可疑。我通常会创建这样的服务;
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 )})
}
}
我认为你可能有更深层次的功能,而你实际上并没有调用它。