I am facing an issue with my custom Google map control in Ionic framework. I want the control to open a modal when clicked, but I keep getting an error "Cannot read property '$$destroyed' of undefined" when I try to do so. On the other hand, another control that clears markers is working perfectly fine.
When I set modal.show
as the function for the button, I encounter the mentioned error upon clicking it. However, if I use modal.show()
, the modal opens automatically when the page loads, which is not desired, and then refuses to open again upon button click.
Controller
.controller('MapCtrl', function(mapServ, mapFact, $ionicModal, $scope){
var vm = this;
var gmap = mapServ.init();
var markers = mapServ;
// Creating 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);
});
// Adding Markers
mapFact.markers($rootScope.rests, gmap).then(function(results) {
mapServ.markers = results;
vm.markers = mapServ.markers;
});
});
From the Factory
// Button Constructor
map.Control = function(controlDiv, text, func) {
// Setting 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);
// Setting 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);
};