[Fixed] how to read a string after each comma using Angular 7

Issue

How to show in Category when I get it from API like Category: "Art,History,Culture",

In .html I used this code

                       <mat-card-actions>
                            <button mat-stroked-button color="primary"
                                style="float: left;">{{ subEntry.Category}}</button>
                        </mat-card-actions>

but show a button Art,History,Culture
I want to show 3 button: button Art , button History , button Culture

Can you share with me any idea please?

Solution

In component.ts file once you get response from API and store it in an array and use *ngFor directive on html to iterate through that array and add buttons accordingly.

//component.ts

categories : string[] = []
apiCall().subscribe(response =>{
   this.categories = response.split(',')
});

// component.html

<div *ngFor='let category of categories'>
 <button mat-stroked-button color="primary"
                                style="float: left;">{{ category }}</button>
</div>

Leave a Reply

(*) Required, Your email will not be published