html - 在切换按钮调用时从 api 接收数据时未显示数据
问题描述
我正在尝试在如下所示的界面上显示控制台中显示的数据。单击切换按钮时会进行 API 调用,但该数据并未加载到 UI 中。
这是我的打字稿代码的样子:
getCardById(cardId) {
this._transactionService.getCardById(cardId).subscribe((response) => {
if (!this._commonService.isEmptyObject(response)) {
var cardById = new CardDecrypt();
cardById.other = new Other();
cardById.maskedCardNumber = this._commonService.decrypt(response?.maskedCardNumber);
cardById.expirationMonth = this._commonService.decrypt(response?.expirationMonth);
cardById.expirationYear = this._commonService.decrypt(response?.expirationYear);
cardById.fourDigit = cardById.maskedCardNumber.slice(cardById.maskedCardNumber.length - 4);
cardById.default = response?.default;
cardById.id = response?.id;
cardById.name = response?.name;
cardById.type = response?.type;
cardById.other.address1 = (response.other.address1 != '') ? this._commonService.decrypt(response?.other.address1) : '';
cardById.other.address2 = (response.other.address2 != '') ? this._commonService.decrypt(response?.other.address2) : '';
cardById.other.city = (response.other.city != '') ? this._commonService.decrypt(response?.other.city) : '';
cardById.other.country = (response.other.country != '') ? this._commonService.decrypt(response?.other.country) : '';
cardById.other.nameOnCard = (response.other.nameOnCard != '') ? this._commonService.decrypt(response?.other.nameOnCard) : '';
cardById.other.number = (response.other.number != '') ? this._commonService.decrypt(response?.other.number) : '';
cardById.other.state = (response.other.state != '') ? this._commonService.decrypt(response?.other.state) : '';
cardById.other.zipcode = (response.other.zipcode != '') ? this._commonService.decrypt(response?.other.zipcode) : '';
console.log(cardById);
}
else {
}
});
}
这就是我存储信息并将其显示在 UI 上的方式:
<div class="payment-history-right ml-auto">
<a class="trigger-dropdown" href="#{{card?.id}}" data-toggle="collapse" data-target="#{{card?.id}}" (click)="getCardById(card?.id)">
<i class="fa fa-chevron-down "></i>
</a>
</div>
<div id="{{card?.id}}" class="payment-history-item-detail collapse">
<div class="row">
<div class="col-md-6">
<div class="ph-item-detail-title">Name on card</div>
<div class="ph-item-detail-desc">{{cardById?.name}}</div>
</div>
<div class="col-md-6">
<div class="ph-item-detail-title">Billing Address</div>
<div class="ph-item-detail-subtitle">{{cardById?.other.nameOnCard}}</div>
<div class="ph-item-detail-desc">
{{cardById?.other.address1}} <br />
{{cardById?.other.address2}}, <br />
{{cardById?.other.city}}, <br />
{{cardById?.other.state}}-{{cardById?.other.zipcode}} <br />
</div>
</div>
</div>
</div>
但是这些值没有显示。
解决方案
原因:view
在从 API 加载数据之前显示。因此,您可以添加*ngIf
指令以等待它。此外,如果您使用了OnPush
策略,则可能需要使用手动更改检测。根据使用情况Toogle showData
。
<div *ngIf="showData" class="payment-history-right ml-auto">
<a class="trigger-dropdown" href="#{{card?.id}}" data-toggle="collapse" data-target="#{{card?.id}}" (click)="getCardById(card?.id)">
<i class="fa fa-chevron-down "></i>
</a>
</div>
</div>
<div id="{{card?.id}}" class="payment-history-item-detail collapse">
<div class="row">
<div class="col-md-6">
<div class="ph-item-detail-title">Name on card</div>
<div class="ph-item-detail-desc">{{cardById?.name}}</div>
</div>
<div class="col-md-6">
<div class="ph-item-detail-title">Billing Address</div>
<div class="ph-item-detail-subtitle">{{cardById?.other.nameOnCard}}</div>
<div class="ph-item-detail-desc">
{{cardById?.other.address1}} <br />
{{cardById?.other.address2}}, <br />
{{cardById?.other.city}}, <br />
{{cardById?.other.state}}-{{cardById?.other.zipcode}} <br />
</div>
</div>
在您的组件文件中:
public showData: boolean = false;
public cardById: any
getCardById(cardId) {
this._transactionService.getCardById(cardId).subscribe((response) => {
if (!this._commonService.isEmptyObject(response)) {
this.cardById = new CardDecrypt();
cardById.other = new Other();
cardById.maskedCardNumber = this._commonService.decrypt(response?.maskedCardNumber);
cardById.expirationMonth = this._commonService.decrypt(response?.expirationMonth);
cardById.expirationYear = this._commonService.decrypt(response?.expirationYear);
cardById.fourDigit = cardById.maskedCardNumber.slice(cardById.maskedCardNumber.length - 4);
cardById.default = response?.default;
cardById.id = response?.id;
cardById.name = response?.name;
cardById.type = response?.type;
cardById.other.address1 = (response.other.address1 != '') ? this._commonService.decrypt(response?.other.address1) : '';
cardById.other.address2 = (response.other.address2 != '') ? this._commonService.decrypt(response?.other.address2) : '';
cardById.other.city = (response.other.city != '') ? this._commonService.decrypt(response?.other.city) : '';
cardById.other.country = (response.other.country != '') ? this._commonService.decrypt(response?.other.country) : '';
cardById.other.nameOnCard = (response.other.nameOnCard != '') ? this._commonService.decrypt(response?.other.nameOnCard) : '';
cardById.other.number = (response.other.number != '') ? this._commonService.decrypt(response?.other.number) : '';
cardById.other.state = (response.other.state != '') ? this._commonService.decrypt(response?.other.state) : '';
cardById.other.zipcode = (response.other.zipcode != '') ? this._commonService.decrypt(response?.other.zipcode) : '';
console.log(cardById);
this.showData = true; // it will update the view
}
else {
}
});
推荐阅读
- kotlin - 错误:类 CoroutinesRoom 中的方法执行不能应用于给定类型
- schema.org - 如何使用 Wikidata 项目 URI 指定博客文章的主题?
- html - 如何将 HTML 表格代码嵌入到 confluence 中
- python-3.x - 我如何在heroku中运行另一个进程
- reactjs - 我想在登录时显示不同的标题
- c++ - 如何在 C++ 中生成特定的迭代器
- npm - 关于 npm 包安装
- node.js - 如何使用集群 MongoDB Atlas 配置连接 Adonis.Js
- stata - 控制多个直方图的布局
- jquery - 在 Angular 中使用 jQuery 来处理事件或所有其他东西是一种好方法或最佳实践吗?