1

I am trying to loop through a comma separated string in Angular 6.

public  getCategory(){
    this.Jarwis.getCategorys().subscribe((data:  Array<object>) => {
    this.categorys  =  data;
    console.log(this.categorys);
});

This is my function which have a console log as

(3) [{…}, {…}, {…}, {…}, {…}, {…}]
 0: {id: 4, category_name: "Agriculture", sub_category_names: "Other Agriculture,Vineyards and Wineries,Greenhouses,Tree Farms and Orchards"}
 1: {id: 5, category_name: "Automotive and Boat", sub_category_names: "Auto Repair and Service Shops,Car Dealerships,Marine/Boat Service and Dealers,Junk and Salvage Yards"}
 2: {id: 13, category_name: "Beauty and Personal care", sub_category_names: "Massage,Tanning Salons,Spas,Hair Salons and Barber Shops"}

I can display category name in view page with the help of

<li *ngFor='let category of categorys'>
  <div>{{ category.category_name }}</div>
</li>

But how can I display sub_category_names in different divs just like this

<div> subcategory_name1 </div>
<div> subcategory_name2 </div>

Please help

1
  • You may need to change your sub_category_names into an array Commented Oct 5, 2018 at 9:26

4 Answers 4

3

You can use a custom pipe to split the array:

@Pipe({
  name: 'splitComma'
})
export class SplitCommaStringPipe implements PipeTransform {
  transform(val:string):string[] {
    return val.split(',');
  }
}

and use it like:

<div *ngFor="let subcategory of category.sub_category_names|splitComma"> 
  {{subcategory}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

2

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>

3 Comments

I wouldn't recommend doing this. Especially with more complex functions... It will be executed on each change detection cycle. The pipe solution is more efficient. (blog.appverse.io/…)
@Andrew change detection will fire only when categorys property will change. In this case change detection will never fire because there is no any event that going to change categorys property. Again if categorys property will change then obviously it will re render and pipe or whatever used here will fire. I'm working on ChangeDetection from Angular 2 and this code will valid in every case and will not cause any issue.
you can see waht I mean here: stackblitz.com/edit/… Open the console and see how many times split is called. Also click the button (nothing changes) and see what happens! The problem is that angular doesn't know that split method return pretty much the same thing (it actually returns a new array each time) so it has to call it.
0

May be you can try this way by using an additional *ngFor

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

https://stackblitz.com/edit/angular-a4s1bq

Comments

0

You can also split the sub-categories on return of the data. And then use *ngFor on the sub-categories. In ES6 this would look like this:

this.categories = data.map((e => {
  return {
     ...e,
     sub_category_names: e.sub_category_names.split(',')
  }
}));

btw. plural of category is categories

https://stackblitz.com/edit/js-rkkyjs

Comments

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.