I'm trying to open an ionic modal when an openlayers feature is selected.
I have injected ModalController but it is undefined when it's used from the listener.
I'm assuming it's because of context being different, but how can I resolve this ?
Should I use eventEmitters or rx/observables or is it possible to correctly attach the listener ?
Here's the code
import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { SelectActionNichoirPage } from '../../pages/modal/select-action-nichoir/select-action-nichoir.page';
@Component({
selector: 'app-openlayers-wrapper',
templateUrl: './openlayers-wrapper.component.html',
styleUrls: ['./openlayers-wrapper.component.scss'],
})
export class OpenlayersWrapperComponent implements OnInit {
selectClick;
constructor(public modalController: ModalController) {
console.log("Construct modal" , this.modalController); // not Undefined
}
initMap() {
this.map = new OlMap({
target: 'map',
layers: [this.layer, observationsVectorLayer],
view: this.view,
});
this.selectClick = new Select({
condition: click
});
this.map.addInteraction(this.selectClick);
console.log("add listen", this.modalController); // not undefined
this.selectClick.on('select', function(e) {
console.log(e.target.getFeatures().getLength() +
' selected features');
console.log("modal ctrl in listener", this.modalController); // UNDEFINED
});
this.selectClick.on('select', this.presentModal);
}
async presentModal() {
console.log("modal ctrl in function", this.modalController); // UNDEFINED
const modal = await this.modalController.create({ // exception : no method create on undefined
component: SelectActionNichoirPage
});
return await modal.present();
}
}
For a quick fix, that can also help you confirm your intuition about the problem, save a reference to your component before declaring the function, something like this,
var self = this;
this.selectClick.on('select', function(e) {
console.log(e.target.getFeatures().getLength() +
' selected features');
console.log("modal ctrl in listener", self.modalController); // <- self
self.presentModal(); <- self
});