Pass a variable from a http call to another

32 views
Skip to first unread message

Charlie Camus

unread,
Apr 25, 2014, 11:12:03 AM4/25/14
to ang...@googlegroups.com
Hi all,

I'm a beginner with Angular JS, and I think I missed something.

Here is my code :

$scope.listAccueil=function(){
       
        $http.get('/findParams')
            .success(function(data){
                $scope.params=data;
            })
     
        console.log($scope.params); // here $scope.params is empty even when '/findParams' bring something

        $http.get('/listHabitats')
            .success(function(data){
                $scope.listHabitats =[];
                for (var habitat in data) {
                    if(habitat.prix>$scope.params.budget){
                        habitat.color='list-group-item-danger';
                        habitat.alert='Budget dépassé';
                    }
                    $scope.listHabitats.push(habitat);
                }
            })
    }

So my problem is, I use $scope.params in the second http call and at that time it is empty. I don't understand why ! listAccueil is in a controller, and the $scope variable is global to all the controller isn't it ?

Thank you

Gabriel Aszalos

unread,
Apr 25, 2014, 11:14:39 AM4/25/14
to ang...@googlegroups.com
What you are looking for is `promises`. $http returns a promise which you can chain on to. Just look into the guide for the $q service and watch some clips on chaining promises (on egghead.io for example). Its a very elegant way of doing what you want and I'm sure you'll find it very useful.

Gabriel Aszalos

unread,
Apr 25, 2014, 11:16:16 AM4/25/14
to ang...@googlegroups.com
Also, you have to understand how an async request works. params is empty because the request is not completed. params will only be available once the success function is called AFTER the request is successful. Like I previously said - you'll have to do a bit of research and spend some time learning how promises and async requests work.

Good luck!

Billy Figueroa

unread,
Apr 25, 2014, 1:24:20 PM4/25/14
to ang...@googlegroups.com
As mentioned above you can either use a .success callback or use a promise with .then to pass data. If you are not familiar with it do some research and study it a little bit

Witold Szczerba

unread,
Apr 27, 2014, 5:32:28 PM4/27/14
to ang...@googlegroups.com

Listen to what other said already. Once you are all ready to asynchronous, you will come to something like this:

$http.get(...)
.then(function(response1) {
  return $http.get(...);
})
.then(function(response2) {
  return $http.get(...);
})
.then(function(response3) {
  return $http.get(...);
})
...you can chain like that as long as you want.
There is no need to return only $http promises. You can return whatever you want, including other promises... or just regular values.

You can also attach error handlers just like with try...catch synchronous equivalent.

Do your async/promises/Q/$q homework and you will discover brand new world :-)

Regards,
Witold Szczerba
---
Sent from my mobile phone.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Charlie Camus

unread,
Apr 28, 2014, 3:16:12 AM4/28/14
to ang...@googlegroups.com
Thanks to all, I will follow your advices and try to learn some more about the 'promises'.

Thanks
Reply all
Reply to author
Forward
0 new messages