Displaying Comma Separated String in Angular 6

Displaying comma separated string in Angular 6

Use following code in your html:

<li *ngFor='let category of categorys'>
<div>{{ category.category_name }}</div>
<div *ngFor="let subCategory of category.sub_category_names?.split(',')">
{{ subCategory }}
</div>
</li>

Angular split comma separated values in data object

validate all the answers , finally landed with this.

Component level
considering the data already in Array , Mat select expects Array.

citydata = this.data.Cities.split(',')

HTML

<mat-select [placeholder]="placeHolder" [formControl]="control" multiple>
<mat-option *ngFor="let item of citydata" [value]="item">{{item}}
</mat-option>

</mat-select>

comma separated data display in angular on the basis of condition

you can leverage last local variable of the *ngFor directive https://angular.io/api/common/NgForOf#local-variables

<span *ngFor="let data of myData; last as isLast">
<img src="assets/myImage.png">
<span>{{data.title}}</span>
<ng-container *ngIf="!isLast">,</ng-container>
</span>

How to print an array in comma separated string in angular html

You can use Array.prototype.join (notice the white space after the comma)

<p class="personaType">{{cardData.names.join(', ')}}</p>

This will print all the elements in the array, separated by a comma and a white space.

How to explode comma separated string to single index array using angular js or java script

use split() to convert a string to an array

var str = "15,16,17"var strArr = str.split(',');console.log(strArr); // gives ["15","16","17"];

comma separated string using ngFor with a condition?

The reason last is not getting applied is because the last element is not of the type "action".

As a Fix, You can create a custom function to filter the initial data first and apply the same logic,

<div *ngFor="let data of filterFunction(responsedata); last as isLast">
<div >
{{ data.name}} {{isLast?'':', '}}
</div>
</div>

DEMO

NgFor with Comma separated Values

You can use string.Split method to create an array of strings as follows and loop over them

 Mystring = '1,5,12,66';
arrString = this.Mystring.split(',');

STACKBLITZ DEMO



Related Topics



Leave a reply



Submit