2

In Angular 2 latest RC context I try to build a view with several tabs. Data come from an API where I need to make calls in two steps to retrieve targeted data.

  • Each tabs contains accordion groups and relate to categories
  • Each accordion groups contains bullet list and relate to categories[i].subtopics

This part works perfectly well.

Things become more tricky when I try to implement the <li>tag with data coming from a second array subTopics. I try several combinations of *ngFor="let services of subTopics without success. What are y options to display the way I want (see comment in HTML template)

My component HTML

<tabset>
<tab heading="{{category.name}}" *ngFor="let category of categories | orderBy" >
    <h2>Content</h2>
    <accordion>
        <accordion-group  heading="{{subtopic.name}}" *ngFor="let subtopic of category.subtopics | orderBy">
            <li>
<!-- Here is where I want to display value for each services.name key related to my subtopic. -->
           </li>
        </accordion-group>
    </accordion>
</tab>

My categories JSON

My categories array structure looks like this. After retrieving this object I iterate over each categories[index].subtopicsin order to generate my subTopics array.

[{
    "code": "FIRST_CATEGORY",
    "name": "First Categories", //Tab heading
    "subtopics": [{
        "code": "CODE1",
        "name": "Name for Code 1" //Accordion group heading
    }, {
        "code": "CODE2",
        "name": "Name For Code 2" //Accordion group heading
    }]
},{
    "code": "SECOND_CATEGORY",
    "name": "Second Categories", //Tab heading
    "subtopics": [{
        "code": "CODE3",
        "name": "Name for Code 3" //Accordion group heading
    }, {
        "code": "CODE4",
        "name": "Name for Code 4" //Accordion group heading
    }]
}]

My subTopics JSON

My subTopics array structure looks like this. I simplify it a lot, as there is many key/value per services.

[{
    "subtopic": "CODE1",
    "services": [{
        "code": "755",
        "name": "The Name of My Code 755" //my <li> Tag
    }, {
        "code": "199",
        "name": "The Name of My Code 199" //my <li> Tag
    }]
}, {
    "subtopic": "CODE2",
    "services": [{
        "code": "761",
        "name": "The Name of My Code 761" //my <li> Tag
    }, {
        "code": "356",
        "name": "The Name of My Code 356" //my <li> Tag
    }]
}]

This array is build using this function where code and index coming from my categoriesarray iteration.

getSubServicePerTopic(code, index){
    this._dxdService.getSubTopics(code, this.regionId)
    .subscribe(subTopics => {
        this.subTopics[index] = subTopics;
    });
}
Benoit
  • 374
  • 4
  • 20
  • thanks @ChristopherMoore I already review that topic. Could it be that I failed to build a proper Pipe()? – Benoit Jan 31 '17 at 17:37
  • `ngFor` works with `Array`. It seems you are using `subTopics[0]` and as you have mentioned that `subTopics` contains an array of object, `subTopics[0]` should contain an object which will not work with `ngFor`. So use `subTopics` instead. – micronyks Jan 31 '17 at 17:38
  • @Benoit Impossible to say without looking at more of your code :) If you don't want to use a pipe, create a function getKeyArray() and call that instead – Christopher Moore Jan 31 '17 at 17:38
  • I'll update my post with more code. – Benoit Jan 31 '17 at 17:43
  • Please show structure of `categories` and `subTopics`. :) Maybe show your `Pipe` too. – slaesh Jan 31 '17 at 18:11
  • @mxii I've update my post with complete overview of my HTML templates and JSON structure for the 2 arrays involved. Hope it makes more sens. Any input will be appreciated. – Benoit Feb 01 '17 at 11:26