Angularjs等到

Dio*_*lor 4 angularjs angularjs-directive angularjs-scope

我有:

$scope.bounds = {}
Run Code Online (Sandbox Code Playgroud)

后来在我的代码中:

$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,
    });
});
Run Code Online (Sandbox Code Playgroud)

你可以在乞讨中看到它们是空的边界但是它们稍后(几毫秒)用一个javascript库(小叶角度)加载.但是,$scope.$on(...)设置了边界之前的运行,因此'west' : $scope.bounds.southWest.lng,返回带有未定义变量的错误.

我想要做的是等待边界(southWestnorthEast)设置然后运行Dajaxice.async.hello(...).

所以我需要"等到边界设置"之类的东西.

Che*_*niv 5

您可以使用$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);
 });
Run Code Online (Sandbox Code Playgroud)