angular - 每次第一个字母更改时,将大写字母添加到角度列表中
问题描述
我有一份建筑师名单。每次建筑师以新字母开头时,我都应该添加一个字母,以构建建筑师列表:
例子:
一个
ABC建筑师北极建筑师
乙
波士顿建筑师
D
荷兰建筑师
等等
我的实际代码:
<ul class="architect-list">
<li *ngFor="let architect of architects">
<div class="single-architect" (click)="selectArchitect(architect.title)" [innerHTML]="architect.title">
</div>
</li>
</ul>
如何在不修改列表的情况下添加字母?
备注:字母不应链接。
解决方案
您可以创建一个返回布尔值的方法,以决定是否应显示占位符字母表。这应该通过将每个架构师名称的第一个字符与组件上的属性进行比较来完成。它还应确保在列表中以新字母开头的名称时立即更新该属性。
试试这个:
<ul class="architect-list">
<ng-container *ngFor="let architect of architects">
<ng-container *ngIf="checkIfNew(architect.title)">
{{ architect.title.charAt(0).toUpperCase() }}
</ng-container>
<li>
<div class="single-architect" (click)="selectArchitect(architect.title)" [innerHTML]="architect.title">
</div>
</li>
</ng-container>
</ul>
组件类:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
currentAlphabet;
architects = [...];
checkIfNew(title: string) {
if(this.currentAlphabet === title.charAt(0).toLowerCase()) {
return false;
} else {
this.currentAlphabet = title.charAt(0).toLowerCase();
return true;
}
}
}
这是您参考的示例 StackBlitz。
推荐阅读
- vue.js - 用户登录成功后导航栏未更新
- python - 如何根据两个熊猫数据帧上的匹配条件添加新列
- javascript - 如何正确地将 html 部分显示到带有背景图像的画布上?
- html - 音频标签下载菜单未显示在正确的位置
- dynamics-crm - Dynamics WebAPI:仅更新更改的字段?
- json - 当键上的类型不匹配时,使 Swift JSONDecoder 不会失败
- php - 多对一关系上的 Doctrine MappingException
- custom-routes - Cakephp 3.7 中的自定义路由以实现 SEO 网址
- android - 后台服务不会使用 Android 传感器永久运行以提供模拟位置数据
- java - org.springframework.dao.InvalidDataAccessApiUsageException:具有该位置 [2] 的参数不存在