Angular 向模式窗口发送参数(角度8)
我在Angular 向模式窗口发送参数(角度8),angular,rest,api,bootstrap-modal,ng-bootstrap,Angular,Rest,Api,Bootstrap Modal,Ng Bootstrap,我在.ts openFormModal(id: number) { console.log(id); const modalRef = this.modalService.open(PartidoComponent); modalRef.componentInstance.id = id; //modalRef.componentInstance.id = id; modalRef.result.then((result) => { c
.ts
openFormModal(id: number) {
console.log(id);
const modalRef = this.modalService.open(PartidoComponent);
modalRef.componentInstance.id = id;
//modalRef.componentInstance.id = id;
modalRef.result.then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
}
在component.html
<button (click)="openFormModal(calendario.fixture_id)">Open Modal</button>
开放模式
我可以看到来自另一个组件的模式总是相同的,因为我无法发送相应的
id
。有人知道如何正确发送参数吗?创建一个名为child
(显示模式)的组件,然后在其Html中:
<ng-template #childmodal let-modal>
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Create New Item</h4>
<button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
<span aria-hidden="true">×</span>
</button>
</div>
<form>
<div class="modal-body">
<div class="form-group row m-b-15">
This is a Modal- Id is {{this.itemId}}
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-outline-primary btn-sm ml-1">Save</button>
<button type="button" class="btn btn-outline-danger btn-sm" (click)="hideModal()">Close</button>
</div>
</form>
</ng-template>
现在,在要显示模态的父组件中:
Html:
<app-child></app-child>
<button type="button" class="btn btn-sm btn-primary" (click)="this.openModal()">
Open Modal
</button>
谢谢你的回答 对我来说,解决办法如下: 此代码是正确的:
modalRef.componentInstance.id = id;
然后我在ngOnInit
中编写了一个console.log
,该值显示正确,然后我将执行API查询的代码以这种方式移动到该部分:
ngOnInit () {
console.log (this.id);
this.activatedRoute.params.subscribe ((params) => {
this.lpefService.getPartidos (this.id)
.subscribe ((data: any) => {
this.partidos = data.api.fixtures;
console.log (this.parts);
});
});
}
我希望它能帮助您,如果有人知道比这个更好的解决方案,请将数据传递到模式窗口取决于模式服务的实现方式。你在模态对话框或引导中使用角材料吗?我使用这个组件
modalRef.componentInstance.id = id;
ngOnInit () {
console.log (this.id);
this.activatedRoute.params.subscribe ((params) => {
this.lpefService.getPartidos (this.id)
.subscribe ((data: any) => {
this.partidos = data.api.fixtures;
console.log (this.parts);
});
});
}