我試圖顯示使用該服務的模態並獲取以下錯誤。我可以成功地從一個按鈕調用服務,但從錯誤處理程序調用時會出現此錯誤。使用BsModalService顯示模態的錯誤
TypeError: Cannot read property 'attachView' of undefined
at ComponentLoader.webpackJsonp.../../../../ngx-bootstrap/component-loader/component-loader.class.js.ComponentLoader.show (vendor.bundle.js:19572)
at BsModalService.webpackJsonp.../../../../ngx-bootstrap/modal/bs-modal.service.js.BsModalService._showBackdrop (vendor.bundle.js:21508)
at BsModalService.webpackJsonp.../../../../ngx-bootstrap/modal/bs-modal.service.js.BsModalService.show
這裏是我的調用代碼:
import { Injectable } from "@angular/core";
import { BsModalService, BsModalRef } from "ngx-bootstrap/modal";
import { MessageBoxComponent } from "../message-box/message-box.component";
@Injectable()
export class NotificationService {
private modalRef: BsModalRef;
constructor(private modalService: BsModalService) {}
showModal(type: string, title: string, message: any) {
this.modalRef = this.modalService.show(MessageBoxComponent);
this.modalRef.content.type = type;
this.modalRef.content.title = title;
this.modalRef.content.message = message.toString();
}
}
和應用程序模塊:
import { NgModule } from '@angular/core';
import { ModalModule } from 'ngx-bootstrap/modal';
import { MessageBoxComponent } from './modules/common/message-box/message-box.component';
@NgModule({
entryComponents: [MessageBoxComponent],
imports: [ModalModule.forRoot()]
//...etc
})
export class AppModule { }
誰能幫助我?
我以前試過,但它沒有工作,因爲它被注入爲空。我現在想出了我的問題,併發布了我如何修復它。不管怎麼說,還是要謝謝你 – Brendan