2016-07-10 51 views
4

我在同步做angularjs函數有一個大問題。 我已經嘗試過承諾和回調,但沒有一個可以工作。同步angularjs函數

initMap().then(function(result){ 
    console.log("in initMap"); 

    getLocation().then(function(result){ 
     console.log("getLocation"); 
     if(result){ 
      getPlaces.getData(map,myLatlng).then(function(data){ 
       Array = data; 
       console.log("markersArray = ", markersArray); 
      }).catch(function(){ 
       console.log('testtesttest'); 
      }) 
     }else{ 
      console.log("error in getLocation"); 
     } 

    }).catch(function(){ 
     console.log("getLocationError"); 
    }) 
}).catch(function(error){ 
    console.log("bbbbb"); 
}) 

函數 'initMap()' 有

{ 
    var defer = $q.defer(); 
    //Codes... 
    defer.resolve(data); 
    return defer.promise; 
} 

,從而功能 '的getLocation' 和.service'getPlaces'

然而,他們都異步進行的。 控制檯打印爲:

in initMap <-- 1 
getLocation <-- 2 
error in getLocation <-- 3 

數1應該不被打印,直到initMap()得到解決。 因此,在解決getLocation之前不應打印第2和第3個數字,並檢查結果爲false或true。

我現在真的處於死路一條。

請幫忙。 任何建議都可以。 示例代碼非常感謝。

預先感謝您。

Pawas

編輯: 每種方法的代碼如下。

噢。我在離子平臺上做這個。 這是否會影響angularjs的工作方式? 以及如何解決該問題?

'initMap'

var mapOptions = { 
     center: myLatlng, 
     zoom: 16, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var mapVar = new google.maps.Map(document.getElementById("map"), mapOptions); 
    $scope.map = mapVar; 

    console.log("initMap");  
    var defer = $q.defer(); 
    defer.resolve('initMap'); 
    return defer.promise; 

'的getLocation'

var defer = $q.defer(); 
var suc = false; 

navigator.geolocation.getCurrentPosition(function(pos){ 
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
    $scope.map.setCenter(myLatlng); 
    suc = true; 

},function(error){ 
    suc = false; 
},{ 
    timeout: 12000 
}); 
defer.resolve(suc); 
return defer.promise; 

'getPlaces':

Sorry, this one I can't post the code. 
+0

在當前的情況下發生了什麼,你只給了預期的結果..你還可以共享代碼'initMap','getLocation'和'getPlaces'嗎? –

+0

它看起來像是根據你的描述做它應該做的。一個問題:'getLocation'中解析的值在'true'時被處理爲錯誤,這是否是正確的行爲? –

+0

Pankaj Parkar:等幾分鐘。我將編輯 –

回答

2

你的問題是,你返回之前解決的承諾。

var defer = $q.defer(); <-- create the promise 
defer.resolve('initMap'); <-- resolve it 
return defer.promise; <-- returns a resolved promise 

因此您的電話.then立即執行。在getCurrentPosition同樣的事情,你總是與價值false

var defer = $q.defer(); 
var suc = false; 

// Here, this is a callback executed asynchronously. So the code continue to executes 
navigator.geolocation.getCurrentPosition(function(pos){ 
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
    $scope.map.setCenter(myLatlng); 
    suc = true; 

},function(error){ 
    suc = false; 
},{ 
    timeout: 12000 
}); 

// This is resolve with the value false from the initialization of the variable above 
defer.resolve(suc); 
// Always returns a resolved promise with the value false 
return defer.promise; 

你的代碼的第一部分似乎是同步解決您的承諾。創建Google地圖對象是同步執行的。你可以將它轉換成一個承諾,但它是沒用的。

對於getLocation,移動異步回調中的解析。

var defer = $q.defer(); 
var suc = false; 

navigator.geolocation.getCurrentPosition(function(pos){ 
    myLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
    $scope.map.setCenter(myLatlng); 
    suc = true; 
    defer.resolve(suc); 

},function(error){ 
    suc = false; 
    defer.reject(suc); 
},{ 
    timeout: 12000 
}); 


return defer.promise; 
+0

God!我不知道我怎麼能感謝你。它真的很沮喪。 (T T) 非常感謝你。 –