css - 在每个部分应用 ngClass
问题描述
我已经完成了所有 UI 部分的实现。看起来
我的数据是
public filters = [
{
tag: 'Year',
label: 'year',
items: [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
},
{
tag: 'Cash On Delivery',
label: 'cash',
items: this.truthyFalsy
},
{
tag: 'In stock',
label: 'stock',
items: this.truthyFalsy
}
];
我的html代码是
<div *ngFor="let filter of filters">
<p ><small>{{filter?.tag}}</small></p>
<div class="line"></div>
<div>
<div class="item" *ngFor="let item of filter?.items">
<button class="btn" [ngClass]="{'btn-active-1':labelType1 === filter.label && selectedItem === item }"
(click)="select(item,filter?.label)">{{item}}</button>
</div>
</div>
</div>
在选定的按钮上,我必须使其处于活动状态。
实际上我的要求是
- 在
year section
我必须让任何一个人活跃 - 在
Cash On delivery
我必须让任何一个人活跃 In stock
我必须让任何一个人活跃起来
在每个部分中,我必须至少使用一个按钮处于活动状态ngClass
解决方案
我在Stackblitz上为您创建了一个示例。
您可以使用let i = index
按索引选择项目并具有默认选定项目。所以,试试这个来选择每个组中的第一个项目:
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
labelType1: string = "";
public selectedItem = { year : 0, cash: 0, stock: 0 };
truthyFalsy: any[] = [true, false];
public filters = [
{
tag: 'Year',
label: 'year',
items: [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
},
{
tag: 'Cash On Delivery',
label: 'cash',
items: this.truthyFalsy
},
{
tag: 'In stock',
label: 'stock',
items: this.truthyFalsy
}
];
select(index, label){
this.selectedItem[label] = index;
}
}
}
app.component.html
<div *ngFor="let filter of filters">
<p><small>{{filter?.tag}}</small></p>
<div class="line"></div>
<div>
<div class="item" *ngFor="let i=index; let item of filter?.items">
<button class="btn" [ngClass]="{'btn-active-1': selectedItem[filter.label] === i}" (click)="select(i,filter?.label)">{{item}}</button>
</div>
</div>
</div>
推荐阅读
- python-3.x - 更改 bincopy 段的地址
- python - 使用 Python Rewrite 从 Discord 下载 PNG/GIF/MP3 文件
- python - Python 自动下载 (ufile.io)
- python - 当它们之间没有间隙时加入字符串变量的分割线
- graph - plantuml - 更好地配置箭头和文本
- python - 将字符串拆分两次
- json - 如何读取静态 json 并在 Angular 中填充评论部分?
- python - 如何将特定的 SQL 语句实现为 SQLAlchemy ORM 查询
- typescript - 错误:“必须在 href 中定义路由器”stenciljs router-v2 测试
- c++ - COM - C++ 调用函数返回 DISP_E_TYPEMISMATCH