【发布时间】:2019-10-23 11:22:12
【问题描述】:
如何将数据从一个组件发送到另一个组件?我有两个组件,我正在使用选择器在第一个组件中显示 html,现在我想将表单中输入的数据发送到第一个组件。
产品组件.html
<form id="form" #form="ngForm" (ngSubmit)="onSubmit(form)">
<label>
<div class="field-heading">Name</div>
<input type="text" [(ngModel)]="product.name" name="productName" class=" theme-input" id="text"
placeholder="Enter product name">
</label>
<product-variant> </product-variant>
</form>
产品组件.ts
export class ProductComponent implements OnInit{
variant = new Variant();
product= new Product();
constructor() { }
ngOnInit() {
}
onSubmit(){
console.log(this.product);
console.log(this.variant);
}
ProductVariant 组件.html
<label>
<div class="field-heading">Starting Inventory</div>
<input type="text" [(ngModel)]="variant.reorderLevel" name="reorder" class=" theme-input" id="text"
placeholder=" Starting Inventory">
</label>
ProductVariant 组件.ts
import { Component, OnInit, ViewChild } from '@angular/core';
@Component({
templateUrl: 'product-variant.component.html',
selector: 'product-variant'
})
export class ProductVariant implements OnInit {
variant = new Variant();
// send data from this component to the product
}
【问题讨论】:
标签: angular typescript