Angularjs are waiting

I have:

$scope.bounds = {}

And later in my code:

$scope.$on('leafletDirectiveMap.load', function(){
    console.log('runs');
    Dajaxice.async.hello($scope.populate, {
                'west' : $scope.bounds.southWest.lng,
                'east': $scope.bounds.northEast.lng,
                'north' : $scope.bounds.northEast.lat,
                'south': $scope.bounds.southWest.lat,
    });
});

The borders you can see when begging are empty, but they are loaded later (a few milliseconds) using the javascript library ( angular flyer ). However, it $scope.$on(...)is executed before the boundaries are set, so it 'west' : $scope.bounds.southWest.lng,returns an error with the variable undefined.

What I want to do is to wait for the border ( southWestand northEast) to be installed, and then run Dajaxice.async.hello(...).

So I need something like "wait until the boundaries are set."

+4
source share
2 answers

$watch, - :

 $scope.$on('leafletDirectiveMap.load', function(){

       $scope.$watch( "bounds" , function(n,o){  

           if(n==o) return;

           Dajaxice.async.hello($scope.populate, {
               'west' : $scope.bounds.southWest.lng,
               'east': $scope.bounds.northEast.lng,
               'north' : $scope.bounds.northEast.lat,
               'south': $scope.bounds.southWest.lat,                
            });

       },true);
 });
+5

, , $watch:

$scope.$watch('bounds',function(newBounds) {
   ...          
});

, , , :

var stopWatching = $scope.$watch('bounds',function(newBounds) {
  if(newBounds.southWest) {
     ...
     stopWatching();
  }
});

: http://plnkr.co/edit/nTKx1uwsAEalc7Zgss2r?p=preview

+3

All Articles