【发布时间】:2022-07-07 19:53:16
【问题描述】:
我有一个父组件,其中包含三个子组件实例。
child-product-detail.component.html
<form id="frmProduct" #frmProduct="ngForm" (ngSubmit)="onSave(frmProduct)">
<ng-content select="[buttons-view]"></ng-content>
<input type="text" id="txtProductName" name="txtProductName" [(ngModel)]="product.productName" />
</form>
child-product-detail.component.ts
onSave(form) {
let isValid = this.validate(form);
if (!isValid) return;
}
parent-product.compoment.html
<child-product-detail [product]="products[0]">
<div buttons-view>
<button type="button" class="btn" (click)="saveProduct(0)" >Save</button>
</div>
</child-product-detail>
<child-product-detail [product]="products[1]">
<div buttons-view>
<button type="button" class="btn" (click)="saveProduct(1)" >Save</button>
</div>
</child-product-detail>
<child-product-detail [product]="products[2]">
<div buttons-view>
<button type="button" class="btn" (click)="saveProduct(2)" >Save</button>
</div>
</child-product-detail>
parent-product.component.ts
saveProduct(productId) {
let productToSave = this.products(productIndex);
// Code required to call onSave method of child component
}
无论如何我可以调用子组件的onSave方法传递它的表单对象吗?
谢谢。
【问题讨论】:
-
也许可以通过使用@ContentChildren 来做到这一点,但如果您发布了您想要完成的任务的完整描述并且您会好多以不同的方式解决了这个实际问题。您正在尝试做一些通常是反模式的事情,原因尚不清楚。
标签: angular typescript