我正在按姓名搜索数据正常,然后尝试获取搜索产品的详细信息(从产品数组中访问单个产品详细信息)。数据将显示在控制台中,但不会显示在我的HTML视图中。 这里我的组件代码
export class SearchdetailComponent implements OnInit {
@Input() product: Product;
ngOnInit() {
this.getComponent();
}
goBack(): void {
this.location.back();
}
private componentUrl = 'HereMyRestServicePostUrlGoes';
constructor(private http:Http, private route: ActivatedRoute,
private location: Location) {
}
getComponent(): Observable<Product[]> {
const name = this.route.snapshot.paramMap.get('name');
let obj = { name: name }
let body = JSON.stringify(obj);
let headers = new HttpHeaders();
var config = {
headers : {
'Content-Type': 'application/json'
}
};
headers = headers.append('Content-Type' : 'application/json');
const url = `${this.componentUrl}`;
return this.http.post<Product[]>(url, body, config).map((res:Response) => res.json()).subscribe(product => {
this.product = product;
console.log(this.Product);
);
}
}
}
HTML代码
<div class="wide">
</div>
<div class="hero-bkg-animated">
<h2>Product Details</h2>
<form class="form-style-9">
<div >
<div>
<div class="table-responsive">
<table *ngIf="product" class="table table-striped">
<tr><td><b>Name</b></td><td>{{product.name}}</td></tr>
<tr><td><b>Address</b></td><td>{{product.address}}</td></tr>
</table>
</div>
<button (click)="goBack()">Go Back</button>
</div>
</div>
</form>
</div>
路由器代码:
{ path: 'detail/:name', component: SearchdetailComponent }
答案 0 :(得分:0)
*ngIf="product"
除非product
是布尔值,否则这并不意味着什么。
你需要在像这样的属性上使用if条件
<div >
<table *ngFor="let p of product" class="table table-striped">
<tr *ngIf="p.name.length>0"><td><b>Name</b></td><td>{{p.name}}</td></tr>
<tr *ngIf="p.address.length>0"><td><b>Address</b></td><td>{{p.address}}</td></tr>
</table>