簡體   English   中英

將內容添加到動態,有角度的Promise創建的元素中

[英]Add Content To Dynamically, Angular Promise Created Elements

我有一個使用Angular Service獲取數據的地圖。 數據被傳遞到一個函數,該函數在其間循環創建標記。 每個循環調用一個函數,該函數為每個標記創建一個彈出窗口,但顯示為空白。

當我將彈出式內容登錄到控制台時,它會在幾秒鍾后顯示,但是我不知道如何將其傳遞到標記器。 我感覺這與我一直在研究的閉包有關。 這是我的代碼:

    //Create the Markers Function
var createMapMarkers = function (data) {
    angular.forEach(data, function (value, key) {

        //Calling the get popup content function here
        var content = getPopupContent(value.AreaName, value.ImageUrl);

        var marker = L.marker([value.Lat, value.Long])
        .bindPopup(content)
        .addTo($scope.map);
    });
}

function getPopupContent(areaName, imageUrl) {
    var response;
        $http.get('/Home/GetPopupContent?areaName=' + areaName + '&imageUrl=' + imageUrl)
        .then(function (response) {
            //This logs the correct content
            console.log(response);
        });
    //But it does not return here
        return response;
} 

您需要從getPopupContent方法返回promise,然后在准備好結果時就可以對其進行處理。 實際上,您只是在調用一個promise並返回undefined

var createMapMarkers = function (data) {
    angular.forEach(data, function (value, key) {

        //Calling the get popup content function here
        getPopupContent(value.AreaName, value.ImageUrl).then(function(content) {
            var marker = L.marker([value.Lat, value.Long])
                         .bindPopup(content)
                         .addTo($scope.map);
        });
    });
}

function getPopupContent(areaName, imageUrl) {
    return $http.get('/Home/GetPopupContent?areaName=' + areaName + '&imageUrl=' + imageUrl);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM