【发布时间】:2018-04-06 12:37:51
【问题描述】:
我在使用 *ngIf 时遇到了一点问题
我的 ws 为我提供所有产品,当 ws 为我提供“无结果”时,我想在我的页面中显示此消息。为此,我尝试以下代码:
products: Product[]=[];
getallproduct() {
this.activatedRoute.params.subscribe(
params => {
this.ws.product(params['id']).subscribe(
products=> {
this.prod= products;
console.log(products) // show all product or No result
}
);
}
);
}
在 hmtl 中,我使用此代码来获取我的所有产品:
<table *ngFor="let item of prod">
<p *ngIf="prod === 0"> No result! </p>
<tr>
<td>{{item.id}}<td>
<td>{{item.name}}<td>
</tr>
</table>
当我在 html 中使用 *ngIf="prod === 0" 时,没有任何反应。当我在 html 中使用 *ngIf="prod !== 0" 时显示 No result! 当我有结果时。当 prod 为空时,我想要此消息。
{StatusCode: 0, StatusMessage: "OK", StatusDescription: "No result"}
拜托,你能建议任何解决方案吗
编辑:
public product(id: string): Observable<Product> {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let urlSearchParams = new URLSearchParams();
urlSearchParams.append('id', id);
urlSearchParams.append('token', this.auth.getCurrentUser().token);
let body = urlSearchParams.toString();
return this.http.post(Api.getUrl(Api.URLS.product), body, {
headers: headers
})
.map((response: Response) => {
let res = response.json();
console.log(res)
if (res.StatusCode === 0) {
return res.StatusDescription;
} else {
return new Product(null);
}
});
}
【问题讨论】:
-
===进行严格的类型检查,请改用==。
标签: angular typescript angular-ng-if