我正在尝试使用 Angular Material 在子表单输入字段中显示父表中的对象。但它不会在输入字段中显示对象值。它只显示<mat-label>
我创建的。我尝试使用属性绑定添加占位符,但这会导致错误。
这是我的代码: parent-template.html
<table class="mat-elevation-z8 table table-primary table-striped overview-no-margin">
<thead>
<tr>
<th scope="col">Product Tag</th>
<th scope="col">Product Status</th>
</tr>
</thead>
<tbody>
<tr (click)="onSelect(product)"
[class]="selectedProduct === product ? selectedProduct : null"
*ngFor="let product of products">
<td>{{product.tag}}</td>
<td>{{product.status}}</td>
</tr>
</tbody>
</table>
<button (click)="addRandomProduct()" class="addProduct btn-lg btn-primary">Add Product</button>
<app-product-detail [showProductDetail]="selectedProduct"></app-product-detail>
父模板.ts
export class ProductEditComponent implements OnInit {
public selectedProduct?: Product;
public products?: Product[];
// public defaultRow?:number = 8;
constructor() { }
ngOnInit(): void {
this.products = [];
for (let i = 0; i < 8; i++){
this.addRandomProduct();
}
}
public onSelect(product: Product): void{
this.selectedProduct = product;
console.log(product)
}
}
子模板.html
<div *ngIf="showProductDetail" id="product-detail">
<form class="product-detail-panel mat-elevation-z8" >
<div id="detail-header">
<span><b>Update scooter </b></span><u>{{showProductDetail.tag}}</u><b> with scooter ID: </b> <u>{{showProductDetail.id}}</u>
</div>
<hr>
<mat-form-field class="product-tag" appearance="fill">
<mat-label>Tag:</mat-label>
<input matInput [(ngModel)]="showProductDetail.tag" >
</mat-form-field>
<mat-form-field class="product-status" appearance="fill">
<mat-label>Product Status:</mat-label>
<mat-select [(ngModel)]="showProductDetail.status" >
<mat-option value="STOCK">STOCK</mat-option>
<mat-option value="OUT OF STOCK">OUT OF STOCK</mat-option>
<mat-option value="COMING SOON">COMING SOON</mat-option>
<mat-option value="NOT DELIVRABLE">NOT DELIVRABLE</mat-option>
<mat-option value="ON SALE">ON SALE</mat-option>
</mat-select>
</mat-form-field>
</div>
子模板.ts
export class ProductDetailComponent implements OnInit {
@Input() panelMessage = 'Select a product from the left panel:';
@Input() showProductDetail?: Product;
constructor() { }
ngOnInit(): void {
}
}