您好我正在尝试获取http请求的状态并有条件地设置变量。我做了一个suball来检查user1是否跟随user2。我的代码看起来像这样。 (为了简洁起见,我删除了foreach函数,该函数需要遍历我之前获得请求的用户列表,这是我必须推送的原因)
$scope.users = [];
var getUser = function(id) {
UserService.GetUserById(id, $localStorage.CurrentUser.auth_token)
.success(function (data) {
data = angular.fromJson(data);
//data model ==> {id: 0, username: "foo"}
//check if 404 or 200 ==> UserService.GetUserFollowers($stateParams.id, data.id)
//if 200 data.is_following = 1; if 404 data.is_following = 0
$scope.users.push(data);
//data model after pushed ==> {id: 0, username: "foo", is_following: 1}
console.log(angular.toJson($scope.users));
}).error(function(error, status) {
alert(status);
console.log(error);
});
};
试过这个没有用的
$scope.users = [];
var getUser = function(id) {
UserService.GetUserById(id, $localStorage.CurrentUser.auth_token)
.success(function (data) {
$scope.data = angular.fromJson(data);
UserService.GetUserFollowers($stateParams.id, $scope.data.id, -1, -1)
.success(function(data, status) {
$scope.status = status;
}).error(function(data, status) {
$scope.status = status;
});
if ($scope.status === 200) {
$scope.data.is_following = true;
}else{
$scope.data.is_following = false;
}
$scope.users.push($scope.data);
console.log(angular.toJson($scope.users));
}).error(function(error, status) {
alert(status);
console.log(error);
});
};
这是我的服务:
this.GetUserFollowers = function (my_id, to_id, begin, end) {
return $http.get($rootScope.endPoint + '/user/' + my_id + '/followers/' + to_id + '/' + begin + '/' + end + '/');
};
!注意 - 如果to_id不是-1,则忽略begin和End
答案 0 :(得分:3)
self.udp = nil;
返回状态代码作为第二个参数。
$http
但是,如果状态是错误状态,例如404,则将调用$http.get(url)
.success(function(data, status) {
alert(status); // HTTP status code of the response
})
.error(function(data, status) {
alert('Error with status code: ' + status);
});
块。