2

我对 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">&times;</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

 }
4

3 回答 3

3

这个问题是因为多个引用或循环引用。

您应该直接从 ngx-bootstrap 导入所有 ngx-bootstrap 组件和服务,ngx-bootstrap而不是转到特定文件。

import { BsModalRef,ModalModule ,BsModalService } from 'ngx-bootstrap';

现场演示

于 2017-08-05T03:48:47.620 回答
1

通过使用解决

从'ngx-bootstrap'导入{BsModalRef};

感谢ngx-bootstapGitHub 上的社区 -问题解决方案

于 2017-08-05T03:31:44.947 回答
1

通过导入解决了

import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
于 2018-11-23T11:40:10.287 回答