我对 Angular 4 很陌生。我一直在研究 Angular 1.5.x,现在使用ngUpgrade
混合方法在 Angular 4 中转换相同的应用程序。我ngx-bootstrap
在我的混合应用程序中使用模态组件来替换现有的模态服务。
我在使用 service 的指南 ngx-bootstrap-modal中发现了一些问题。
这个说法有问题If you passed a component to .show() you can get access to opened modal by injecting BsModalRef
我在这里复制相同的示例,
export class ModalContentComponent {
public title: string;
public list: any[] = [];
constructor(public bsModalRef: BsModalRef) {} // How do you get bsModalRef here
}
我尝试运行这个例子,但我从来没有进入bsModalRef
打开的模式。
我也尝试bsModalRef
通过content
,但它适用于一种模式并且无法使用嵌套模式。
有没有其他方法可以bsModalRef
通过 ModalContentComponent
?
在这里找到完整的例子,
import { Component } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
@Component({
selector: 'demo-modal-service-component',
templateUrl: './service-component.html'
})
export class DemoModalServiceFromComponent {
bsModalRef: BsModalRef;
constructor(private modalService: BsModalService) {}
public openModalWithComponent() {
let list = ['Open a modal with component', 'Pass your data', 'Do something else', '...'];
this.bsModalRef = this.modalService.show(ModalContentComponent);
this.bsModalRef.content.title = 'Modal with component';
this.bsModalRef.content.list = list;
setTimeout(() => {
list.push('PROFIT!!!');
}, 2000);
}
}
/* This is a component which we pass in modal*/
@Component({
selector: 'modal-content',
template: `
<div class="modal-header">
<h4 class="modal-title pull-left">{{title}}</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="bsModalRef.hide()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<ul *ngIf="list.length">
<li *ngFor="let item of list">{{item}}</li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" (click)="bsModalRef.hide()">Close</button>
</div>
`
})
export class ModalContentComponent {
public title: string;
public list: any[] = [];
constructor(public bsModalRef: BsModalRef) {} // How do you get bsModalRef here
}