This plunker should open up a modal when the button is pressed. I have extended the existing ngx-modal but it throws error: Cannot read property 'nativeElement' of undefined.
Having looked at the console this is because "modalRoot" should be programatically assigned as a ViewChild handler for the modal. It doesn't seem to get defined when extended even though I've added the super() to my constructor, any ideas?
//our root app component
import {Component, NgModule, HostListener, ElementRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ModalModule} from "ngx-modal";
import { Modal } from "ngx-modal";
@Component({
selector: 'ext-ngx-modal',
template: `<ng-content></ng-content>`,
})
export class NgxModalComponent extends Modal {
constructor() {
super();
}
openExt():void {
this.open();
}
@HostListener('document:keydown', ['$event'])
onkeydown(ev: KeyboardEvent) {
console.log("this.isOpened: " + this.isOpened;
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}} I am </h2>
<div class="row container-fluid">
<button (click)="myExtNgxModal.openExt()"> open my modal</button>
<ext-ngx-modal #myExtNgxModal>
<modal>
<modal-header>
<h1>Modal header</h1>
</modal-header>
<modal-content>
Press F12 see the console...press a key while modal open
</modal-content>
<modal-footer>
<button class="btn btn-primary" (click)="myModal.close()">close</button>
</modal-footer>
</modal>
</ext-ngx-modal>
</div>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
}
@NgModule({
imports: [ BrowserModule, ModalModule ],
declarations: [ App, NgxModalComponent ],
exports: [ NgxModalComponent ],
bootstrap: [ App ]
})
export class AppModule {}