代码之家  ›  专栏  ›  技术社区  ›  Smith James

Angular2 ng引导:同一个模式窗口需要2个不同的按钮

  •  0
  • Smith James  · 技术社区  · 7 年前

    angular2+ng引导+引导Beta 4

    我的问题是:

        <ng-template #contact let-c="close" let-d="dismiss">
      <div class="modal-header">
        <h4 class="modal-title">Modal1</h4>
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>I'd like my 2 differents buttons to open the same modal window</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark" (click)="c('Close click')">Close</button>
      </div>
    </ng-template>
    
    <button class="btn btn-lg btn-outline-primary" (click)="open(contact)">Contact button but with different text and style and same popup</button>
    

    http://plnkr.co/edit/pmRdVLyDLkJkkIPLItAL?p=preview

    非常感谢您的帮助和建议

    1 回复  |  直到 7 年前
        1
  •  0
  •   Gosha_Fighten    7 年前

    你可以移动你的 contact input property .

    @Input() content: ElementRef;
    

    因此,现在您可以选择将标记传递给子级:

    <ngbd-modal2 [content]="contact"></ngbd-modal2>
    

    open() {
      this.modalService.open(this.content);
    }
    

    plunk .

    您可以创建一个服务,该服务将在定义模式内容的组件和调用模式对话框的组件之间共享。服务只需要公开一个可观察对象和一种提高可观察对象的方法。

    服务代码:

    subscription = new Subject();
    showModal() {
        this.subscription.next();
    }
    

    因此,具有模式对话框内容的组件可以订阅 subscription 要显示模式对话框:

    @ViewChild('contact') modalContent: ElementRef;
    constructor(private modalService: ModalService, private ngbModal: NgbModal) {
        modalService.subscription.subscribe(() => {
          this.ngbModal.open(this.modalContent)
        })
    }
    

    因此,模态对话框消费者只需要调用 showModal

    constructor(private modalService: ModalService) {}
    open() {
      this.modalService.showModal();
    }
    

    查看更新的 plunk .