【发布时间】:2020-11-09 10:45:58
【问题描述】:
我已经实现了一个 Angular 10 应用程序,它包含一个父组件 customer 和子组件 createcustomer。父组件包含一个列出所有客户的表格,单击表格上的编辑链接将数据传递给子组件。如您所见,调用了试图初始化@input 变量的父方法editCustomer。 目前我的子组件@Input 值未定义。我可以在父组件中看到对象的值。不知道为什么输入变量没有被初始化。有人可以解释一下吗
父组件
import { Component, OnInit } from '@angular/core';
import { CustomerService } from '../../services/customer/customer.service';
import { ICustomer } from '../../models/customer.model';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { CreateCustomerComponent } from '../create-customer/create-customer.component';
@Component({
templateUrl: './customer.component.html',
styleUrls: ['./customer.component.scss']
})
export class CustomerComponent implements OnInit {
customerDetails: ICustomer[];
customer: ICustomer;
public modal: any;
totalItems: number;
maxSize = 5;
term: string;
isEdit: boolean;
constructor(private customerService: CustomerService,
public modalService: NgbModal) {
}
ngOnInit(): void {
this.getCustomerDetails();
}
getCustomerDetails(): void {
this.customerService.getCustomerDetails()
.subscribe(data => {
this.customerDetails = data;
this.totalItems = data.length;
}
);
}
public addCustomer(): void {
this.isEdit = false;
this.openModal();
}
public editCustomer(custDetails: ICustomer): void {
this.isEdit = true;
this.openModal();
this.customer = custDetails;
console.log(this.customer);
}
private openModal(): void {
this.modal = this.modalService.open(CreateCustomerComponent,
{ size: 'fullscreen', centered: true, backdrop: 'static' });
}
}
父组件html
<div class="container">
<div class="row">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search Here" [(ngModel)]="term">
</div>
<div>
<button class="btnAddCustomer" (click)="addCustomer()"> Add Customer (Template Driven) </button>
</div>
</div>
<div class="row">
<div class="col-12">
<div *ngIf="customerDetails" style="height: 700px; overflow-y:auto">
<table id="customerdetails-table" class="table table-bordered table-striped">
<thead>
<th>Customer</th>
<th>Company Name</th>
<th>Contact Name</th>
<th>Contact Title</th>
<th>Address</th>
<th>City</th>
<th>Region</th>
<th>Postal Code</th>
<th>Country</th>
<th>Phone</th>
<th>Fax</th>
<th>View Order</th>
</thead>
<tbody>
<tr *ngFor="let custDetails of customerDetails">
<td>{{custDetails.customerId}}</td>
<td>{{custDetails.companyName}}</td>
<td>{{custDetails.contactName}}</td>
<td>{{custDetails.contactTitle}}</td>
<td>{{custDetails.address}}</td>
<td>{{custDetails.city}}</td>
<td>{{custDetails.region}}</td>
<td>{{custDetails.postalCode}}</td>
<td>{{custDetails.country}}</td>
<td>{{custDetails.phone}}</td>
<td>{{custDetails.fax}}</td>
<td>
<a [routerLink]="'/customers'" class="table-row-action edit-action" (click)="editCustomer(custDetails)">
edit
</a>
</td>
</tr>
</tbody>
</table>
</div>
<pagination [totalItems]="totalItems" [itemsPerPage]="5" [maxSize]="maxSize"></pagination>
</div>
</div>
</div>
<app-create-customer [isEdit]="isEdit" [customer]="customer"></app-create-customer>
子组件
import { Component, OnInit, Input } from '@angular/core';
import { CustomerService } from '../../services/customer/customer.service';
import { ICustomer } from '../../models/customer.model';
@Component({
selector: 'app-create-customer',
templateUrl: './create-customer.component.html',
styleUrls: ['./create-customer.component.scss']
})
export class CreateCustomerComponent implements OnInit {
@Input() isEdit: boolean;
@Input() customer: ICustomer;
constructor(private customerDetailsService: CustomerService) { }
ngOnInit(): void {
console.log('Inside create customer component' + this.customer);
}
}
【问题讨论】:
-
如果将 isEdit 设置为 false 或 true 会发生什么?我的意思是在声明的同时
-
考虑为您的问题制作一个stackblitz 示例,以便问题更加明显。
-
考虑在您的输入angular.io/guide/… 中创建一个setter 或拦截更改angular.io/guide/…
标签: angular