嗨,我試圖得到一個http請求的狀態並有條件地設置一個變量。我正在做一個subcall來檢查user1是否跟隨user2。我的代碼看起來像這樣。 (爲簡便起見我剪的是通過我以前得到要求我之所以要推的用戶的列表需要循環在foreach功能)if語句來檢查http請求的狀態AngularJS
$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
你問如何編寫'GetUserFollowers'函數,以便返回這些狀態碼?如果這是您正在嘗試編制邏輯的功能,那麼現在查看您的代碼會很有幫助...... – Claies
@Claies檢查編輯..由於某種原因編輯後的代碼將其設置爲true或假錯誤 – teddybear123
什麼,就像'$ http'調用的狀態返回的東西不是'HTTP 200'和'$ scope.data.is_following'仍然以某種方式結束了嗎? – Claies