gpt4 book ai didi

twitter-bootstrap - ngx-bootstrap 如何从另一个组件打开模式?

转载 作者:太空狗 更新时间:2023-10-29 17:13:16 25 4
gpt4 key购买 nike

我想做的是从另一个组件打开一个模式,但是当 component1. html 加载是因为包含了 childModal。我怎样才能摆脱这些错误并正确实现?

component1.html

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

组件1.ts

 @ViewChild('childModal') childModal: ModalComponent;

模态.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-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">&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>

官方文档:

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

关于twitter-bootstrap - ngx-bootstrap 如何从另一个组件打开模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46698126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com