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

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

CSS: Pass a variable in nth-child with angular

问题

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

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

ts模型:

  1. @Input() public limit: number

scss:

  1. .collapsed-list {
  2. max-height: 15rem;
  3. overflow: hidden;
  4. .expandable:nth-child(${limit}) {
  5. color: red;
  6. }
  7. }

感谢您的帮助。

英文:

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:

  1. @Input() public limit: number

scss:

  1. .collapsed-list {
  2. max-height:15rem;
  3. overflow: hidden;
  4. .expandable:nth-child(${limit}) {
  5. color: red;
  6. }
  7. }

Thanks for help

答案1

得分: 1

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

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

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

英文:

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

  1. .max {
  2. color: red;
  3. }
  4. &lt;div class=&quot;collapsed-list&quot; *ngFor=&quot; counter; let index &quot;&gt;
  5. &lt;div [class]=&quot;index === limit ? &#39;max&#39; : &#39;&#39;&quot;
  6. &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:

确定