Or you could use the promise from $http
Pete
...from my mobile.
That's working, thanks!And the "$scope.watch("vio", function() {}" can do the same,right?--I still hope to find a way not have to use a callback, just do:doSimethingWith($scope.vio)But I think it may be impossible.
On Wednesday, July 18, 2012 10:10:41 PM UTC+8, Ricardo Bin wrote:You can do this using events.When the ajax request is done, send a $broadcast to InnerCtrl with data value. The InnerCtrl need have an observer $on too.I simulate your async request on a button click in this fiddle: http://jsfiddle.net/ricardohbin/xxXMu/
Em quarta-feira, 18 de julho de 2012 10h38min56s UTC-3, Freewind escreveu:When a page is loaded, there is a very important object should be retrieved from $http, and all other logic are based on it. So my code is:$http(..., function(data) {$scope.vio = data;// other logic are all based on the "vio"});Now I split the code into several controllers, a root one, and some inner ones. To simplify the code, I want to get the "vio" in root controller, and other controllers are just use it. The code will be:function RootCtrl($scope, $http) {$http(..., function(data) {$scope.vio = data;// other logic are all based on the "vio"});}function InnerCtrl($scope, $http) {// do something with $scope.vio// e.g console.log($scope.vio);}But since $http is asynchronous, I can't just use `$scope.vio` which may be 'undefined'.How to solve this problem? How to make the $http call synchronous, or wait "vio" non-undefined in InnerCtrl?
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/I-wKgzeSQDUJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.
Visit this group at http://groups.google.com/group/angular?hl=en-US.