2017-10-11 120 views
2

我想要做的是從另一個組件打開一個模式,但是我一直得到這個錯誤TypeError: Cannot create property 'validator' on string 'test1'component1.html加載,因爲包括childModal。我如何擺脫這些錯誤並正確實施?ngx-bootstrap如何從另一個組件打開模式?

component1.html

<button type="button" class="btn btn-outline-primary btn-lg" (click)="modal.showModal()">Test 
...... 
<child-modal #childModal ></child-modal> 

component1.ts

@ViewChild('childModal') childModal: ModalComponent; 

modal.html

<div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog> 
    <div class="modal-dialog modal-lg"> 
     <div class="modal-content"> 
      <input type="text" formControl="test1"> 
      <input type="text" formControl="test2"> 
      </div> 
    </div> 
</div> 

modal.component.ts

constructor(private modalService: BsModalService) { 
    this.form = new FormGroup({ 
     'test':      new FormControl(), 
     'test2':     new FormControl() 
     }) 
} 
+0

你可以在plunker中重現它嗎? – yurzui

回答

3

如果你想從另一個組件打開一個模態分量,那麼這是NGX-引導做的正確方法:

import { Component } from '@angular/core'; 
import { BsModalService } from 'ngx-bootstrap/modal'; 
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class'; 

/* This is the Component from which we open the Modal Component */ 
@Component({ 
    selector: 'my-component', 
    templateUrl: './service-component.html' 
}) 
export class MyComponent { 
    bsModalRef: BsModalRef; 
    constructor(private modalService: BsModalService) {} 

    public openModalWithComponent() { 
    /* this is how we open a Modal Component from another component */ 
    this.bsModalRef = this.modalService.show(ModalContentComponent); 
    } 
} 

/* This is the Modal Component */ 
@Component({ 
    selector: 'child-modal', 
    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"> 
     ... 
    </div> 
    <div class="modal-footer"> 
     <button type="button" class="btn btn-default" (click)="bsModalRef.hide()">Close</button> 
    </div> 
    ` 
}) 
export class ChildModalComponent { 
    constructor(public bsModalRef: BsModalRef) {} 
} 

被調用模態組件的模板

<button type="button" class="btn btn-primary" (click)="openModalWithComponent()">Create modal with component</button> 

所以,你應該包括模態分量模板是這樣的:

<child-modal #childModal ></child-modal> 

正式開通了l doc:

https://valor-software.com/ngx-bootstrap/#/modals#service-component

+0

如何從另一個組件的HTML頁面調用'openModalWithComponent()'?它是否必須存在於同一個組件中才能夠像這樣調用它? – derrickrozay

+0

正如我在回答中所顯示的,在另一個組件的HTML頁面中,您可以觸發方法的執行(在我的示例中,該方法是通過單擊「

+0

它不起作用。我想從我的上面的代碼調用this.bsModalRef = this.modalService.show(ModalComponent);'它只是顯示一個覆蓋,但實際的頁面不顯示。在你上面的例子中,不同文件中的組件都是?多數民衆贊成我如何設置它我不想單個文件,因爲我將有3種不同的模式 – derrickrozay

相關問題