Angular 2+ 添加ng-bootstrap模态到组件库
Angular 2+ add ng-bootstrap Modal to component library
我正在使用 ng-bootstrap,我想向我构建的组件库中添加一个可重用的模式。
我有我的模板
<p>
<ng-template #messageModal let-closeModal="close('Cross click')">
<div id="resultModal">
<div class="modal-header">
<h4 class="mt-3">{{header}}
</h4>
<button id="messageModalClose" type="button" class="close" aria-label="Close" (click)="closeModal">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{message}}
</div>
</div>
</ng-template>
</p>
和组件
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ms-modal',
templateUrl: './ms-modal.component.html',
styleUrls: ['./ms-modal.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class MsModalComponent implements OnInit {
@Input() header: string;
@Input() message:string;
private _modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true
};
constructor(private modalService: NgbModal) { }
ngOnInit() {
}
OpenModal(_messageModal) {
this.modalService.open(_messageModal, this._modalOptions);
}
}
我为 header 和消息添加了@Inputs
我的组件库构建良好
在我导入组件库的应用程序中,我添加了
模态标签
<ms-modal *ngIf="showModal==true" [header]="header" [message]="message">
</ms-modal>
当我设置 showModal=true 时没有任何反应。
showModal:any = false;
this.showModal = "true";
我想我不确定如何正确连接它以在我的各种应用程序中使用我的组件库中的模式。
您需要使用布尔值将 showModal 设置为 true,例如:
this.showModal = true;
而不是用字符串值设置它,就像你在做的那样:
this.showModal = "true";
因为您的 *ngIf="showModal==true"
使用布尔值 true
而不是字符串值 "true"
来有条件地显示您的模式。
好的,我已经开始工作了,但我有几处错误。
请记住,我在组件库中工作,而不仅仅是应用程序。
在我的组件库中...
我有我的模板
<div class="modal-header">
<h4 class="mt-3">
{{header}}
</h4>
<button id="messageModalClose" type="button" class="close" aria-label="Close"
(click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{message}}
</div>
和组件
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { NgbModal, NgbModalOptions, NgbActiveModal } from '@ng-bootstrap/ng-
bootstrap';
@Component({
selector: 'ms-modal',
templateUrl: './ms-modal.component.html',
styleUrls: ['./ms-modal.component.scss'],
//encapsulation: ViewEncapsulation.None,
})
export class MsModalComponent implements OnInit {
@Input() header: string;
@Input() message:string;
private _modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true
};
constructor(public activeModal: NgbActiveModal) { }
ngOnInit() {
}
closeModal() {
this.activeModal.close();
}
}
我将我的组件导入我的应用程序模块
从 'ms-components';
导入 {msModalModule}
我将 'msModalModule' 添加到 @NgModule 导入数组并添加组件
被模块引用到 entryComponents
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
FormsModule,
msModalModule
],
bootstrap: [AppComponent],
entryComponents:[MsModalComponent]
})
导出 class AppModule {}
现在在我的 app.component.ts 我添加了一个函数来处理打开模态框并传入输入值
OpenModal(header,message){
***NgbModalOptions is optional
const modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true,
};
const modalRef = this.modalService.open(MsModalComponent,modalOptions)
modalRef.componentInstance.header = header;
modalRef.componentInstance.message = message;
}
我正在使用 ng-bootstrap,我想向我构建的组件库中添加一个可重用的模式。
我有我的模板
<p>
<ng-template #messageModal let-closeModal="close('Cross click')">
<div id="resultModal">
<div class="modal-header">
<h4 class="mt-3">{{header}}
</h4>
<button id="messageModalClose" type="button" class="close" aria-label="Close" (click)="closeModal">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{message}}
</div>
</div>
</ng-template>
</p>
和组件
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ms-modal',
templateUrl: './ms-modal.component.html',
styleUrls: ['./ms-modal.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class MsModalComponent implements OnInit {
@Input() header: string;
@Input() message:string;
private _modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true
};
constructor(private modalService: NgbModal) { }
ngOnInit() {
}
OpenModal(_messageModal) {
this.modalService.open(_messageModal, this._modalOptions);
}
}
我为 header 和消息添加了@Inputs 我的组件库构建良好
在我导入组件库的应用程序中,我添加了 模态标签
<ms-modal *ngIf="showModal==true" [header]="header" [message]="message">
</ms-modal>
当我设置 showModal=true 时没有任何反应。
showModal:any = false;
this.showModal = "true";
我想我不确定如何正确连接它以在我的各种应用程序中使用我的组件库中的模式。
您需要使用布尔值将 showModal 设置为 true,例如:
this.showModal = true;
而不是用字符串值设置它,就像你在做的那样:
this.showModal = "true";
因为您的 *ngIf="showModal==true"
使用布尔值 true
而不是字符串值 "true"
来有条件地显示您的模式。
好的,我已经开始工作了,但我有几处错误。
请记住,我在组件库中工作,而不仅仅是应用程序。
在我的组件库中...
我有我的模板
<div class="modal-header">
<h4 class="mt-3">
{{header}}
</h4>
<button id="messageModalClose" type="button" class="close" aria-label="Close"
(click)="closeModal()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{message}}
</div>
和组件
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { NgbModal, NgbModalOptions, NgbActiveModal } from '@ng-bootstrap/ng-
bootstrap';
@Component({
selector: 'ms-modal',
templateUrl: './ms-modal.component.html',
styleUrls: ['./ms-modal.component.scss'],
//encapsulation: ViewEncapsulation.None,
})
export class MsModalComponent implements OnInit {
@Input() header: string;
@Input() message:string;
private _modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true
};
constructor(public activeModal: NgbActiveModal) { }
ngOnInit() {
}
closeModal() {
this.activeModal.close();
}
}
我将我的组件导入我的应用程序模块 从 'ms-components';
导入 {msModalModule}我将 'msModalModule' 添加到 @NgModule 导入数组并添加组件 被模块引用到 entryComponents
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
HttpClientModule,
FormsModule,
msModalModule
],
bootstrap: [AppComponent],
entryComponents:[MsModalComponent]
}) 导出 class AppModule {}
现在在我的 app.component.ts 我添加了一个函数来处理打开模态框并传入输入值
OpenModal(header,message){
***NgbModalOptions is optional
const modalOptions: NgbModalOptions = {
backdrop: 'static',
keyboard: false,
size: 'lg',
centered: true,
};
const modalRef = this.modalService.open(MsModalComponent,modalOptions)
modalRef.componentInstance.header = header;
modalRef.componentInstance.message = message;
}