I am working with an object array
that requires me to dynamically add an image icon based on the type of credit card.
Typescript file
icon: any;
savedCreditCard =
[
{
cardExpiryDateFormat: "05/xx",
cardNumberLast: "00xx",
cardId: "xxx",
cardType: "Mastercard",
cardExpiryDate: "xx05",
paymentChannelId: 9,
cardNumberMasked: "512345XXXXXXXXXX"
},
{
cardExpiryDateFormat: "11/xx",
cardNumberLast: "58xx",
cardId: "xxx",
cardType: "Amex",
cardExpiryDate: "xx11",
paymentChannelId: 16,
cardNumberMasked: "379185XXXXXXXXX"
}
]
ngOnInit() {
this.savedCreditCard.forEach((x => {
if (x.cardType === 'Mastercard') {
this.icon = '../../assets/svg/logo-mastercard.svg';
} else if (x.cardType === 'Amex') {
this.icon = '../../assets/svg/icon-amex.svg';
}
})
);
}
When binding the image dynamically in the HTML template based on the type of credit card, I encountered an issue where I only get the same icon regardless of whether it is a Mastercard or Amex. Here is what I have tried:
HTML file
<div class="flex-float">
<div class="float-end">
<img class="select--icon" [src]="icon" />
<p class="selected--desc is-hidden-mobile-xs">
{{ selectedCard.cardType }}
</p>
</div>
</div>
I attempted to reproduce the issue on StackBlitz but it does not support static images. Does anyone have any suggestions on how to solve this problem?