search

Home  >  Q&A  >  body text

angular.js - angularjs2 how to nest multiple levels of loops

In angularjs2, *ngFor is an instruction to implement loops. In practice, it was found that multiple levels of loops cannot be nested.
If you want to implement more than 2 nested loops, how should you implement it

怪我咯怪我咯2832 days ago800

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 17:09:24

    You can implement nested loops with multiple ngFors. It mainly depends on how your data format corresponds.

    Data format:

    // demo.json
    
    {
        "nav": [{
            "title": "一级导航1",
            "subs": [
                { "txt": "二级导航1", "link": "#" },
                { "txt": "二级导航2", "link": "#" },
            ]
        }, {
            "title": "一级导航2",
            "subs": [
                { "txt": "二级导航2", "link": "#" },
            ]
        }, {
            "title": "一级导航3",
            "subs": [
                { "txt": "二级导航3", "link": "#1" },
            ]
        }]
    }
    

    Code example:

    // 导航带子菜单循环例子
    <ul>
        <li *ngFor="let nav of navs">  // 这里是外层循环
            <strong>{{nav.title}}</strong>
            <a *ngFor="let sub of nav.subs" href="{{sub.link}}"> // 这里是内层循环
                {{sub.txt}}
            </a>
        </li>
    </ul>

    reply
    0
  • Cancelreply