你不能用ngx-bootstrap做到这一点,但你可以用@ng-bootstrap/ng-bootstrap。
示例用法Service
this.dialogService.show("Logged Out","You are out of here");
示例用法route guard
async canActivate() {
if (!this.auth.loggedIn()) {
await this.loginDialogService.confirm();
return this.auth.loggedIn();
}
return true;
}
服务的源代码。
对话框组件.ts
import { Injectable } from '@angular/core';
import {Component} from '@angular/core';
import {NgbModal, ModalDismissReasons,NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-dialog',
templateUrl: './dialog.component.html',
styleUrls: ['./dialog.component.css']
})
export class DialogComponent {
constructor(private modalService: NgbModal,public activeModal: NgbActiveModal) {}
public title: string;
public message: string;
}
@Injectable()
export class DialogService {
constructor(private modalService: NgbModal) {}
public show(title: string, message:string) {
const modalRef = this.modalService.open(DialogComponent);
modalRef.componentInstance.title = title;
modalRef.componentInstance.message = message;
return modalRef.result;
}
}
dialog.component.html
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">{{title}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="activeModal.close()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{message}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" (click)="activeModal.close()">Ok</button>
</div>
您还需要将其entryComponent
添加到
entryComponents: [DialogComponent,LoginDialogComponent]
给你@NgModule
归功于 long2know好的博客。