angular - 产品折扣显示和隐藏Angular中的错误
问题描述
我只想在折扣百分比大于 0 时才显示折扣百分比和原始价格(删除)。我从服务器动态获取这两个值,因为我使用 ngFor 循环。我只是将它绑定到我的 html 中。为了实现这一点,我使用了 *ngIf 指令。但即使它为零,它也会显示所有折扣值。如何使用 ngIf 来实现这一点。任何人都可以帮助解决这个问题。在此先感谢..我不想显示 0% 的折扣以及删除价格。
我的 component.html 是:
<div *ngIf="discountPercent!==0">
<div class="col-md-3" *ngFor="let product of productList">
<figure class="card card-product">
<div class="starburst discount" id="star" #rotateEl>
<span></span>
<span>{{product.Discount}}%</span>
</div>
<div class="img-wrap">
<a routerLink="/product-details"><img src="assets/{{ product.Image }}"></a>
</div>
<div class="parent">
<div class="block-ellipsis">
<span>{{product.ProductName}}</span>
</div>
</div>
<div class="bottom-wrap">
<div class="price-wrap h5">
<span class="price-new">Rs.{{product.DiscountedPrice}}</span>
<del class="price-old">Rs.{{product.OriginalPrice}}</del>
</div>
</div>
</figure>
</div>
</div>
我的 component.ts 文件是:
export class ProductsComponent implements OnInit {
discountPercent = 0;
ngOnInit() {
this.productsService.getProducts()
.then((response:Response) => {
console.log(response);
this.productList = response;
this.discountPercent = this.productList.Discount;
})
.catch(err => console.log(err));
}
}
解决方案
看起来discountPercent
变量是没有必要的。试试这个。
<div class="col-md-3" *ngFor="let product of productList">
<figure class="card card-product">
<div *ngIf="product.Discount" class="starburst discount" id="star" #rotateEl>
<span></span>
<span>{{product.Discount}}%</span>
</div>
<div class="img-wrap">
<a routerLink="/product-details"><img src="assets/{{ product.Image }}"></a>
</div>
<div class="parent">
<div class="block-ellipsis">
<span>{{product.ProductName}}</span>
</div>
</div>
<div class="bottom-wrap">
<div class="price-wrap h5">
<span class="price-new">Rs.{{product.DiscountedPrice}}</span>
<del *ngIf="product.Discount" class="price-old">Rs.{{product.OriginalPrice}}</del>
</div>
</div>
</figure>
</div>
推荐阅读
- python - Bottlenose ItemLookup 生成 HTTP 响应代码 410 Gone Error
- java - Java 2D 游戏照明,alpha 的“问题”
- sql-server - 重构包含大量引用的大型 SQL 表
- python - 根据字典列表中的不同键获取多个列表
- sql - 日期差异 - 解析今天的日期以捕获财政年度的结束
- flutter - 有没有一种方法可以从一个页面切换到另一个页面?
- python - 运行 python 脚本时如何抑制警告?
- javascript - else 运行 5 次,5 次后抛出错误
- python - 使用 Python 从动态 HighCharts 图中抓取数据
- java - Javalin 网络服务器从另一个网络服务器加载配置