angular.js - angularjs2 如何嵌套多层循环
怪我咯
怪我咯 2017-05-15 17:07:24
0
1
724

在angularjs2中,*ngFor是实现循环的指令,实践的时候,发现无法嵌套多层的循环。
如果要实现嵌套2个以上的循环,应该怎么实现呢

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
过去多啦不再A梦

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>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!