【发布时间】:2020-08-21 09:40:47
【问题描述】:
我正在尝试在选择 openlayers 功能时打开离子模式。
我已经注入了 ModalController,但是当它从侦听器中使用时它是未定义的。
我假设这是因为上下文不同,但我该如何解决这个问题?
我应该使用 eventEmitters 还是 rx/observables 还是可以正确附加监听器?
这是代码
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();
}
}
【问题讨论】:
标签: ionic-framework openlayers angular8 ionic5