CSS:在Angular中使用变量传递给nth-child

huangapple go评论57阅读模式
英文:

CSS: Pass a variable in nth-child with angular

问题

我想将来自模型ts的变量传递给nth-child,但我不知道如何做。

现在我的代码看起来像这样:

ts模型:

@Input() public limit: number

scss:

.collapsed-list {
  max-height: 15rem;
  overflow: hidden;

  .expandable:nth-child(${limit}) {
    color: red;
  }
}

感谢您的帮助。

英文:

I want to pass a variable that comes from the model ts into a nth-child, but I don’t know how to do this.
For now my code looks like this:

ts model:

@Input() public limit: number

scss:

.collapsed-list {
  max-height:15rem;
  overflow: hidden;

  .expandable:nth-child(${limit}) {
     color: red;
  }
}

Thanks for help

答案1

得分: 1

一个解决方法是在计数器等于限制时从模板中添加一个类。

.max {
   color: red;
}
<div class="collapsed-list" *ngFor="let index of counter; let index">
  <div [class]="index === limit ? 'max' : ''">
  </div>

请注意,我已经为代码中的HTML和CSS进行了适当的翻译,不包括代码部分。

英文:

A workaround is to add a class from the template when counter === limit.

.max {
   color: red;
}
    
&lt;div class=&quot;collapsed-list&quot; *ngFor=&quot; counter; let index &quot;&gt;
  &lt;div [class]=&quot;index === limit ? &#39;max&#39; : &#39;&#39;&quot;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月8日 23:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定