Hi i'm trying to get the status of an http request and conditionally set a variable. I'm making a subcall to check if user1 is following user2. My code looks like this. (for brevity i cut the foreach function that needs to loop through a list of users that i have previously get requested reason why i have to push)
$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);
});
};
Tried this didn't work
$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 is my service:
this.GetUserFollowers = function (my_id, to_id, begin, end) {
return $http.get($rootScope.endPoint + '/user/' + my_id + '/followers/' + to_id + '/' + begin + '/' + end + '/');
};
!NOTE - begin and End are ignored if to_id is not -1
GetUserFollowersfunction so that it returns these status codes? If that is the function that you are trying to work out the logic for, it would be helpful to see the code you have for it now....$httpcall comes back something other thanHTTP 200and$scope.data.is_followingstill ends up true somehow?