ngx-bootstrap如何从另一个组件打开模式?
我想要做的是从另一个组件打开一个模式,但是我一直得到这个错误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()
})
}
如果你想从另一个组件打开一个模态分量,那么这是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">×</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
如何从另一个组件的HTML页面调用'openModalWithComponent()'?它是否必须存在于同一个组件中才能够像这样调用它? – derrickrozay
正如我在回答中所显示的,在另一个组件的HTML页面中,您可以触发方法的执行(在我的示例中,该方法是通过单击“
它不起作用。我想从我的上面的代码调用this.bsModalRef = this.modalService.show(ModalComponent);'它只是显示一个覆盖,但实际的页面不显示。在你上面的例子中,不同文件中的组件都是?多数民众赞成我如何设置它我不想单个文件,因为我将有3种不同的模式 – derrickrozay
你可以在plunker中重现它吗? – yurzui