1

My goal is to get the array of objects of any index from the services array of arrays, and cycle through that array of objects through *ngFor

FILE.TS

services:  Array<Servizio[]> = [];

FILE.HTML this is what I'd like to be able to do , but nothing shows in my list

  <li *ngFor="let servizio of services[0]"> 
                                <a href="#">{{servizio.name}}</a>
                            </li>

3 Answers 3

1

ngFor works on an array, you are trying to use over first element of the array which is an object, you should do

 <li *ngFor="let servizio of services"> 
      <a href="#">{{servizio.name}}</a>
 </li>
Sign up to request clarification or add additional context in comments.

Comments

1

I post the solution to my problem in case it's helpful to anyone :

<ul class="nav nav-list">
                                <span *ngFor="let servizio of services; let idx = index">
                                    <span *ngIf="idx  === 2">
                                        <li *ngFor="let ser of servizio">
                                            <a href="#">{{ser.name}}</a>

                                        </li>
                                    </span>
                                </span>
                            </ul>

Comments

0

Try like this :

my example :

template

<ul>
    <li *ngFor="let servizio of services[0]?.subMenuItem"> 
        <a href="#">{{servizio.name}}</a>
    </li>
</ul>

typescript :

services: Array<any[]> = [{
    "name": "xyz",
    "subMenuItem": [{
        "name": "abc"
    }, {
        "name": "cde"
    }],
    "icon": "home"
}, {
    "name": "pqr",
    "subMenuItem": [{
        "name": "abc"
    }, {
        "name": "cde"
    }],
    "icon": "home"
}];

2 Comments

Thank you for your answer , what I'm trying to do is to access through *ngFor the first array of objects of the type Servizio from the services array and cycle through the elements of that array , any idea on how to ?
can you provide example data for services ?

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.