2016-04-23 50 views
0

我有一個自定義控件的谷歌地圖,我希望它點擊時打開離子模式。我有另一個控制從地圖清除標記,它工作正常。從Google地圖自定義控件打開離子模式的問題

如果我把modal.show作爲按鈕的函數,當我點擊它的時候,我得到了「無法讀取屬性」$ $$'undefined'。

如果我把modal.show()模式打開時頁面加載,我不想要的,然後將不會再次打開時,我點擊按鈕。

控制器

.controller('MapCtrl', function(mapServ, mapFact, $ionicModal, $scope){ 
    var vm = this; 

    var gmap = mapServ.init(); 
    var markers = mapServ; 

    // Create modal and Custom Map Controls 
    $ionicModal.fromTemplateUrl('templates/modal.html', { 
    scope: $scope 
    }).then(function(modal) { 
    vm.modal = modal; 

    var controlContainer = document.createElement('div'); 
    var clearControl = new mapFact.Control(controlContainer, 'Clear Map', mapFact.clearMap); 
    var locControl = new mapFact.Control(controlContainer, 'Locations', vm.modal.show()); 

    controlContainer.index = 1; 
    gmap.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlContainer); 
    }); 

    // Create Markers 
    mapFact.markers($rootScope.rests, gmap).then(function(results) { 
    mapServ.markers = results; 
    vm.markers = mapServ.markers; 
}); 
}); 

從工廠

// Button Constructor 
map.Control = function(controlDiv, text, func) { 
    // Set CSS for the control border. 
    var controlUI = document.createElement('div'); 
    controlUI.style.backgroundColor = '#ed5929'; 
    controlUI.style.border = '2px solid #ed5929'; 
    controlUI.style.borderRadius = '3px'; 
    controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)'; 
    controlUI.style.cursor = 'pointer'; 
    controlUI.style.marginBottom = '22px'; 
    controlUI.style.marginRight = '22px'; 
    controlUI.style.marginTop = '22px'; 
    controlUI.style.textAlign = 'center'; 
    controlUI.title = 'Click to toggle markers'; 
    controlDiv.appendChild(controlUI); 

    // Set CSS for the control interior. 
    var controlText = document.createElement('div'); 
    controlText.style.color = 'rgb(255,255,255)'; 
    controlText.style.fontWeight = 'bold'; 
    controlText.style.fontFamily = 'Roboto,Arial,sans-serif'; 
    controlText.style.fontSize = '16px'; 
    controlText.style.lineHeight = '38px'; 
    controlText.style.paddingLeft = '5px'; 
    controlText.style.paddingRight = '5px'; 
    controlText.innerHTML = text; 
    controlUI.appendChild(controlText); 
    controlUI.addEventListener('click', func); 
}; 
+0

請澄清,你想究竟達到什麼樣的輸出?首先,你說「我希望它在點擊時打開離子模式」,但你也說「如果我把modal.show()模式打開,當頁面加載,我不想要」。而且,你還可以在使用'modal.show()'的時候分享錯誤日誌嗎? :) – Teyam

+0

對不起,如果我不清楚。我希望按鈕打開模式,但我不希望它在頁面加載時自動打開。當我使用'modal.show()'時,沒有任何錯誤只是在頁面加載時打開模式,然後在按下按鈕時沒有任何反應。 –

回答

0

有作爲Ionic - Javascript Modal給出離子實施模式有兩種方式。

您可以添加單獨的模板,也可以將其添加到常規HTML文件頂部的腳本標記中。首先,您需要將您的模態連接到控制器,然後創建模態。之後,創建將被觸發的功能。最後,創建一個按鈕來觸發modal.show()打開模式。

的HTML代碼示例:

<button class = "button" ng-click = "openModal()"></button> 
+0

謝謝你的建議。儘管如此,我已經能夠打開離子模式了。問題在於將'modal.show()'方法附加到Google地圖按鈕上。我的其他谷歌地圖按鈕工作正常,但打開模式不能按預期工作。 –