2016-12-11 37 views
-1

我需要訂購我的執行。我需要確保我的COMPLETE coords()方法完成,一旦它被調用。如何添加承諾或$ q到它?我試着在控制檯上打印,發現coords()函數的最後一行執行後,coords中的每個循環都完成了。看下面的代碼。我真的很新的角度幫助,請

var appa = angular.module('appa',['firebase','uiGmapgoogle-maps']); 
 
appa.controller('mainCtrl', function($firebaseObject,$scope,$q) { 
 

 
       $scope.coords = function(){ 
 
       
 
       var ref = firebase.database().ref(); 
 
       var latArray = []; 
 
       var lngArray = []; 
 
       var cenlat; 
 
       var cenlng; 
 
        var marker = []; 
 
       ref.once("value") 
 
       .then(function(snapshot) 
 
       { 
 
       snapshot.forEach(function(child) 
 
       { 
 

 
        latArray.push(child.child("Lat").val()); 
 
        console.log(child.child("Lat").val()); 
 
        lngArray.push(child.child("Long").val()); 
 
        var mark = { 
 
        id: child.child("Id").val(), 
 
        coords: { 
 
        latitude: child.child("Lat").val(), 
 
        longitude: child.child("Long").val() 
 
        }, 
 
        options: { title: child.child("Alt").val() } 
 
        }; 
 
        marker.push(mark); 
 
CONSOLE.LOG("wWHY IS THIS PRINTED aFTER??? AND HOW TO HANDLE THIS ??"); 
 
       }); 
 

 
        cenlat = (Math.max.apply(Math,latArray)+Math.min.apply(Math,latArray)/2); 
 
        cenlng = (Math.max.apply(Math,lngArray)+Math.min.apply(Math,lngArray)/2); 
 

 
       }); 
 
       $scope.map.center.latitude = cenlat; 
 
       $scope.map.center.longitude = cenlng; 
 
CONSOLE.LOG("wWHY IS THIS PRINTED BEFORE??? AND HOW TO HANDLE THIS ??"); 
 
}); 
 
       }; 
 

 
       $scope.map = { 
 
       center: 
 
       { 
 
        latitude: 51, 
 
        longitude: 4 
 
         }, 
 
       zoom: 2 
 
         }; 
 

 
     $scope.coords(); 
 
     
 

 

 
    });

+1

你是否注意到在片段中的語法錯誤?你發佈的代碼是無效的javascript,你真的應該修復這個問題,併發布適當格式的代碼,因爲它代表了你的代碼更難以閱讀,當然這不是你寫代碼的方式嗎?並且** CONSOLE.LOG **無論如何都會失敗,它的** console.log ** - javascript不是基本的,它是區分大小寫的 –

+0

nemoAsad,您通過收集證據完成了95%的調試 - 兩條消息是記錄的順序錯誤 - 所以你知道代碼執行的時間。你所需要做的就是對代碼進行隨機播放,以便'$ scope.map'語句在正確的時間執行。這並不複雜。 –

+0

一旦這個問題得到解決,還有更多.... (1)你的平均值計算似乎是錯誤的;你很可能想要'(max + min)/ 2',而不是'(max + min/2)'。 (2)記住lat和lng的範圍從-180到+180。 (max + min)/ 2'對於任何一組貨幣和跨越Grenwich Meridian的lng都是可以的,但對於跨越國際日期線的lngs,「(max + min)/ 2」會給出意想不到的結果不足以說「永遠不會發生」,因爲有一天它會!)。 (3)最大值和最小值的簡單算術平均值有利於離羣值;考慮一個「重心」算法。 –

回答

0

您使用的火力,可以輕鬆地檢查返回的兒童將它們存儲在一個變量。然後在forEach循環中遞減變量。

//initialized var a, is having child count 
 
a = snapshot.numChildren();

和下面的完整代碼將創建有序執行,並有一個承諾的基本實現。我已經添加了console.log來顯示順序。我希望它有助於一些如何。

//complete code 
 

 
var appa = angular.module('appa',['firebase','uiGmapgoogle-maps']); 
 
appa.controller('mainCtrl', function($firebaseObject,$scope,$q){ 
 

 
       $scope.coords = function(){ 
 
var q = $q.defer(); 
 
       var ref = firebase.database().ref(); 
 
       var latArray = []; 
 
       var lngArray = []; 
 
       var cenlat; 
 
       var cenlng; 
 
       var a="0"; 
 
       var marker = []; 
 

 
       ref.once("value") 
 
       .then(function(snapshot) 
 
       { 
 
       a = snapshot.numChildren(); 
 
       console.log(a); 
 
       snapshot.forEach(function(child) 
 
       { 
 
        latArray.push(child.child("Lat").val()); 
 
        console.log(child.child("Lat").val()); 
 
        lngArray.push(child.child("Long").val()); 
 
        var mark = { 
 
        id: child.child("Id").val(), 
 
        coords: { 
 
        latitude: child.child("Lat").val(), 
 
        longitude: child.child("Long").val() 
 
        }, 
 
        options: { title: child.child("Alt").val() } 
 
        }; 
 
        a= a-1; 
 
        console.log("this is current"+a); 
 
        marker.push(mark); 
 
       }); 
 
console.log("hi"+a); 
 
console.log("going for cenlat"+cenlat); 
 
      cenlat = (Math.max.apply(Math,latArray)+Math.min.apply(Math,latArray)/2); 
 
console.log("done with cenlat"+cenlat); 
 
console.log("going for cenlng"+cenlng); 
 
      cenlng = (Math.max.apply(Math,lngArray)+Math.min.apply(Math,lngArray)/2); 
 
console.log("done with cenlng"+cenlng); 
 
      $scope.map.center.latitude = cenlat; 
 
      $scope.map.center.longitude = cenlng; 
 
      if(a==0){ 
 
      q.resolve('resolved'); 
 
      } 
 
      else{ 
 
      q.reject('rejected'); 
 
      } 
 
       }); 
 

 
       }; 
 
       $scope.map = { 
 
       center: 
 
       { 
 
        latitude: 51, 
 
        longitude: 4 
 
         }, 
 
       zoom: 2 
 
         }; 
 

 
    $scope.coords().then(
 
     function(v){ 
 
console.log(v); 
 
     }, 
 
    function(err){ 
 
console.log(err); 
 
    }); 
 

 
    });

+1

請至少修復縮進! –

+0

我知道OP要求與'$ q'有關的事情,但這不是必需的。我想上面的if(a == 0)(...)塊應該表明一些東西,但是* what * exactly!?在目前的形式下,它什麼都沒有展示,除了爲下一個電費單增加一點外,什麼都不做。 –