我在Angularjs中有关于$ http的问题。我正在逐步构建系统寄存器,我需要在第一步调用$ http到Facebook并接收从Facebook返回的结果,所以如果有错误,则在第二步显示此错误。我希望在第一步显示此错误。我将Angularjs与Coffeescript一起使用!
### step one ###
#check username
if $scope.username == ""
$scope.errorMessage.push "please enter username"
$http(
method: "GET"
url: "https://graph.facebook.com/" + identifier
).success((data, status, headers, config) ->
... do something...
).error((data, status, headers, config) ->
... if there is error
$scope.errorMessage.push "error"
# break if has errors
if $scope.errorMessage.length > 0
$scope.scrollTo("back-to-top")
return false
# if everthing ok -> continue next step
return true
### step two ####
...do something...
答案 0 :(得分:1)
有一个'promises'概念,由angularjs中的$ http服务返回。我建议你从here探索'承诺' 和其他在线博客。
您可以按如下方式使用它:
$http.get('https://api.xyz.com/users/abc/gists')
.then(function(response) {
$scope.gists = response.data;
});
答案 1 :(得分:0)
看看$ http响应结构:
$http.get('/someUrl').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
正如您所看到它使用回调结构,我认为重新检查$http documentation会对您有所帮助。