Aishwat Singh
Aishwat Singh

Reputation: 4459

Angular 2 material nested md-list-item

Using Angular 2 with material design, trying to get nested lists in sidenav I have code like

<md-sidenav #sidenav class="sidenav" mode="over" opened>
    <md-nav-list>
           <md-card class="user-card">
               <md-card-header>
                 <div md-card-avatar class="user-avatar"></div>
               </md-card-header>
           </md-card>
      <md-divider></md-divider>
      <md-list-item *ngFor="let category of ategories">
         <a md-line>{{ category.name }}</a> 
      </md-list-item>
    </md-nav-list>
</md-sidenav>

which works fine and looks something like

enter image description here

Now When i try to nest it, like

<md-sidenav #sidenav class="sidenav" mode="over" opened>
    <md-nav-list>
           <md-card class="user-card">
               <md-card-header>
                 <div md-card-avatar class="user-avatar"></div>
               </md-card-header>
           </md-card>
      <md-divider></md-divider>
      <md-list-item *ngFor="let category of ategories">
         <a md-line>{{ category.name }}</a>
         <md-list-item *ngFor="let subcategory of category.subcategories">
            <a md-line>{{ subcategory.subcategory }}</a>
         </md-list-item>
      </md-list-item>
    </md-nav-list>
</md-sidenav>

It appears like

enter image description here

I want to achieve nested list, probably collapsible. Any idea what am i doing wrong or how to approach this ?

Upvotes: 8

Views: 9399

Answers (2)

Layesh Chowdhury
Layesh Chowdhury

Reputation: 31

For Angular 1.0 use this snippet:

<md-list>
    <div ng-repeat="category in categories">
        <md-list-item>{{category.name}}</md-list-item>
        <md-list style="margin-left:50px;">
            <div ng-repeat="subcategory in category">
                <md-list-item>{{ subcategory.name}}</md-list-item>
            </div>
        </md-list>
    </div>
</md-list>

Upvotes: 3

Aishwat Singh
Aishwat Singh

Reputation: 4459

Ok, figured it out, if someone in future gets stuck like this.

Do no *ngfor on md-list-item, rather do it on div, like this

            <md-list>
                <div  *ngFor="let category of practice_categories">
                    <md-list-item>{{category.category}}</md-list-item>
                    <md-list style="margin-left:30px;">
                          <div *ngFor="let subcategory of category.subcategories">
                            <md-list-item>{{ subcategory.subcategory }}</md-list-item>
                          </div>
                    </md-list>
                </div>
            </md-list>

which produces something like

enter image description here

Hope this helps someone, someday

Upvotes: 17

Related Questions