2

I have an array of person objects that I displayed in HTML using ngFor Angular 5.

persons:Array<Person>=[];

the Person objects contains an Array of Role object described as below:

export class Role{
    id:string
    label:string;   
}

export class Person{
   // many attributes
   roles:Array<Person>;

}

In the HTML i am using ngFor to fetch the persons Array in div tag. and I want to get string separated comma of the roles's label for each person and display it in div.

I want something like that:

<div *ngFor='let person of persons'>
   <p> {{ person.roles.label.join(',') }} </p>
</div>

Thanks for help

3 Answers 3

2

You can just use join on the roles in the markup

<div *ngFor='let person of persons'>
   <p> {{ person.roles.join(',') }} </p>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering my issue, but this not what I mean roles contains label and id. An i want string separated comma of label attribute @Suren Srapyan
@Suren_Srapyan I mean roles contains label and id. An i want string separated comma of label attribute
0

You can map your roles to role names first and then join them together.

<div *ngFor='let person of persons'>
    <p> {{ person.roles.map(role => role.label).join(',') }} </p>
</div>

If roles can be undefined or null you should also guard against that case.

<div *ngFor='let person of persons'>
    <p> {{ (person.roles || []).map(role => role.label).join(',') }} </p>
</div>

1 Comment

Hmm, I am not entirely sure this will work, as this is using arrow function (the role => role.label part) and I am not sure if this is supported in angular template expression syntax. If this does not work you might have to transform your data in your component or create a custom pipe in order to do the transformation.
0

This code relves my probelm:

 <div>
    <ng-container *ngFor="let role of person.roles"> 
        {{role.label}} + ","
    </ng-container>
 </div>

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.