As other answers have identified, you need to split your text. My preference for this would be to create in interface that models the different parts of the income text.
export interface Income {
amount: string;
currencySymbol: string;
text: string;
}
In your component or service (wherever makes sense when it comes to reusing the interface), you would map the text to the interface. This is where the complexity lies. I will show a version of using it in the component for simplicity. In reality you would do this in your service for reusability.
incomes: Income[];
ngOnInit() {
this.service.getIncomeTexts().subscribe((texts: string[]) => {
this.incomes = texts.map(x => this.mapTextToIncome(x));
});
}
private mapTextToIncome(text: string): Income {
// this regex will match a string that ends with a dollar symbol
// followed by numbers or commas
// You could extend the possible currency symbols if required
const parts: string[] = /^(.+)(\$)([\d+,]+)$/.exec(text);
return {
amount: parts[3],
currencySymbol: parts[2],
text: parts[1]
};
}
And then it becomes trivial to use in your HTML:
<ul *ngIf="incomes">
<li *ngFor="let income of incomes">
<span>{{income.text}}</span>
<span class="strong">{{income.currencySymbol}}</span>
<span>{{income.amount}}</span>
</li>
</ul>
I have left amount as a string in my example, but you might want to parse it and treat it as a number so that you can apply your own formatting if you wish.
DEMO: https://stackblitz.com/edit/angular-do6joa
Regex demo: https://regex101.com/r/e4nLLO/2
Of course, the correct answer is that your API should return data in a better format :)