0

I have a problem about $http in Angularjs. I'm building system register step by step, I need to call $http to Facebook and receive result returned from Facebook at step one, so if there are error, this error is shown at step two. I want that this error will be shown at step one. I use Angularjs with 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...
2
  • Is this call wrapped in a service or is it straight up as is? I can't see an issue with this as is. Can you elaborate a bit more to the specific issue and expected outcome? Commented Jun 10, 2015 at 2:46
  • Thank for your watching! Yes. I added more some information about system that I'm building to register an account step by step. Code above is in step one! After I receive result from facebook api, if there is error, $scope.errorMessage can't push at step one. Because of $http, $scope.errorMesage show this error at step two. It don't right with logic, beause this error is in step one. Commented Jun 10, 2015 at 3:13

2 Answers 2

1

There is a concept of 'promises' which is being returned by $http service in angularjs. I would recommend that you explore the 'promises' from here and other blogs online.

You can use it as follows :

$http.get('https://api.xyz.com/users/abc/gists')
.then(function(response) {
  $scope.gists = response.data;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the $http response structure:

$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.
  });

As you can see it uses a callback structure, I think rechecking the $http documentation will be helpful for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.