我尝试做的是从另一个组件打开一个模态,但是当TypeError: Cannot create property 'validator' on string 'test1'
加载时我会一直收到此错误component1.html
,因为包含了childModal
。如何摆脱这些错误并正确实施?
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 :(得分:11)
如果您尝试从另一个组件打开模态组件,那么这是使用ngx-bootstrap执行此操作的正确方法:
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>
官方文件:
https://valor-software.com/ngx-bootstrap/#/modals#service-component
答案 1 :(得分:0)
If you use same component then,
HTML #
<button (click)="openModalWithComponent()">open</button>
// for display purpose
<ng-template #template>
<div class="modal-body">
</div>
</ng-template>
COMPONENT #
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
import { BsModalService } from 'ngx-bootstrap/modal';
import { ViewChild, ElementRef } from '@angular/core';
bsModalRef: BsModalRef;
constructor(private modalService: BsModalService) {}
@ViewChild('template') elementView: ElementRef;
openModalWithComponent() {
this.bsModalRef = this.modalService.show(this.elementView);
// this.bsModalRef.content.closeBtnName = 'Close';
// (click)="bsModalRef.hide()"
}
答案 2 :(得分:-1)
您只需要将子组件添加到模块中的entryComponents中,如下所示:
entryComponents: [
childComponent
]
对我有用