特定条件的ngstyle

发布于 2025-02-04 23:12:07 字数 222 浏览 5 评论 0原文

<h4 *ngFor="let x of textAddedList" 
    [ngStyle]="{backgroundColor:textAddedList.length>2?'grey':'transparent'}">
    {{x}}
</h4>

我只想在两个条目后才应用灰色样式,但是当我应用此逻辑时,它适用于第二行之后。提前致谢。

<h4 *ngFor="let x of textAddedList" 
    [ngStyle]="{backgroundColor:textAddedList.length>2?'grey':'transparent'}">
    {{x}}
</h4>

I want to apply the grey styling only after the 2 entries, but when I apply this logic it applies to all instead after 2nd line. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2025-02-11 23:12:07

使用index*ngfor中使用。由于数组的索引始于0,对于第二行之后的行,索引大于1:

<h4
  *ngFor="let x of textAddedList; let i = index"
  [ngStyle]="{ backgroundColor: i > 1 ? 'grey' : 'transparent' }"
>
  {{ x }}
</h4>

示例stackblitz demo

输出

Work with index in *ngFor. As the index of array starts with 0, for the rows after the second row, the index are greater than 1:

<h4
  *ngFor="let x of textAddedList; let i = index"
  [ngStyle]="{ backgroundColor: i > 1 ? 'grey' : 'transparent' }"
>
  {{ x }}
</h4>

Sample StackBlitz Demo

Output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文