1

I am trying to display data showed in the console on my interface shown below. The API call is made when the toggle button is clicked, but that data is not being loaded into the UI.

enter image description here

This is what my typescript code looks like:

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 {

    }
  });
}

This is how I am storing the information and displaying it on the 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>

But the values are not displaying.

2
  • Can you attach your code to the question or create a Minimal Reproducible Example on StackBlitz? It is not recommended to post the code as images as mentioned in How to ask article. Commented Jun 30, 2021 at 5:56
  • Are card and cardById properties of your component? Commented Jun 30, 2021 at 6:20

1 Answer 1

1

The reason: view is displayed before the data is being loaded from the API. So, you can add the *ngIf directive to wait for it. Also, if you have used OnPush strategy, you might need to use manual change detection. Toogle showData as per the usage.

<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>

In your component file:


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 {

  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thankyou so much for help but the issue was i was using cardbyid as local variable after declaring it public and using it with this.cardbyid issue got resolved. Thanks Apoorva

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.