angular - Handle ul menu with angular 7
问题描述
I am working on angular 7 project and have ul list which is populated dynamically from server side.
I got the items no problem my problem is I need to close the ul item and reopen it after 4 li item.
How can I make that in angular using the next example?
<ul class="col-12 col-md-4 col-sm-6 list-unstyled">
<li *ngFor="let footerPage of footerExtraPages; let i = index+1"><a routerLink="page/{{footerPage.id}}/{{footerPage.slug}}">{{footerPage.title}}</a></li>
</ul>
解决方案
我想有几种方法可以解决它
一种是编写一个 getter 来获取元素:
html
<li *ngFor="let footerPage of getFirstFour()">
<a routerLink="page/{{footerPage.id}}/{{footerPage.slug}}">
{{footerPage.title}}
</a>
</li>
ts
public function getFirstFour() {
return this.footerExtraPages.map((item, i) => {
if (i <= 4) {
return item;
}
})
}
然后创建一个新ul
的,相应地得到其余的
为了避免不得不依赖模板 ( let i = index+1
) 中的索引,您也可以使用一个函数来使索引保持一致:
public function getFirstFour() {
return this.footerExtraPages.map((item, i) => {
if (i <= 4) {
return { index: index + 1, ...item};
}
})
}
public function getRest() {
return this.footerExtraPages.map((item, i) => {
if (i > 4) {
return { index: index + 1, ...item};
}
})
}
请注意,如果 item 对象附加了一个 id,上面的代码可能会导致错误,如果您删除扩展运算符,您将能够保留两者,但也意味着您必须重新建模模板以更深层次.
推荐阅读
- reactjs - 从 react Jhipster App 显示用户信息
- flutter - Flutter:如何更改 TextField 中光标的高度
- regex - 在使用 Sublime Text 3 编辑器打开的 txt 文件中使用正则表达式查找重复项
- php - 将对象转换为数组后奇怪的问号符号
- xml - 如何从 XML 文件中提取 IP?
- python - 在json中条带化尾迹空格
- php - PHP | 我如何让我的 php 脚本更新程序检查远程文本文件中丢失的版本?
- .net - 如何使用 .NET Framework 上的 SQLite EF Core 数据库提供程序打包单文件可执行文件?
- html - 用于在参数名称和值之间生成带有特殊字符和前缀的 URL 的 HTML 表单
- c# - 如何在 C# 中的 for...if 循环中降低圈复杂度