简体   繁体   English

Angular 2 根据 ngFor 索引生成类名

[英]Angular 2 Generate Class Name Based on ngFor Index

I'm running into a problem with creating a dynamic class name based on the Angular 2 ngFor loop index.我遇到了基于 Angular 2 ngFor 循环索引创建动态类名的问题。 I had to use the following syntax because Angular 2 does not like ngFor and ngIf on the same element.我不得不使用以下语法,因为 Angular 2 不喜欢同一个元素上的 ngFor 和 ngIf。

With this syntax, how can I create a dynamic class name with the value of index at {{index}} .使用此语法,如何使用{{index}}处的 index 值创建动态类名。 I know this isn't proper A2 code, but I put it in my code example to show you where I would like the value to appear.我知道这不是正确的 A2 代码,但我将它放在我的代码示例中以向您展示我希望该值出现的位置。

<div class="product-detail__variants">
    <template ngFor #variant [ngForOf]="variants" #index="index">
        <div *ngIf="currentVariant == index">
            <div class="product-detail-carousel-{{index}}">

            </div>
        </div>
    </template>
</div>

The value "variants" is an empty array of a set length.值“variants”是一个设定长度的空数组。 "variant" thus has no value.因此,“变体”没有价值。

"currentVariant" is a number that by default equals 0. “currentVariant”是一个默认为 0 的数字。

EDIT : This code above is correct.编辑:上面的这段代码是正确的。 I had another extraneous error that I thought was connected to this code.我有另一个我认为与此代码有关的无关紧要的错误。

I don't really understand your problem ;-)我真的不明白你的问题;-)

There are two ways to set classes for a specific element:有两种方法可以为特定元素设置类:

  • The way you do with curly brackets:你用大括号做的方式:

     @Component({ selector: 'my-app', template: ` <div class="product-detail__variants"> <template ngFor #variant [ngForOf]="variants" #index="index"> <div *ngIf="currentVariant == index"> <div class="product-detail-carousel-{{index}}"> Test </div> </div> </template> </div> `, styles: [ '.product-detail-carousel-2 { color: red }' ] })

    Test is displayed only for the third element (index 2) and in red. Test仅针对第三个元素(索引 2)显示为红色。

  • As suggested by @Langley using the ngClass directive正如@Langley 使用ngClass指令所建议的那样

    import {NgClass} from 'angular2/common'; @Component({ selector: 'my-app', template: ` <div class="product-detail__variants"> <template ngFor #variant [ngForOf]="variants" #index="index"> <div *ngIf="currentVariant == index"> <div class="product-detail-carousel-{{index}}"> Test </div> </div> </template> </div> `, styles: [ '.product-detail-carousel-2 { color: red }' ], directives: [ NgClass ] })

    The different is that you need to specify the NgClass directive within the providers attribute of your component.不同之处在于您需要在组件的providers属性中指定 NgClass 指令。 Again Test is displayed only for the third element (index 2) and in red.再次Test仅显示第三个元素(索引 2)并显示为红色。

Your following sentences: "The value variants is an empty array of a set length. variant thus has no value. currentVariant is a number that by default equals 0.".您的以下句子:“值variants是一个设置长度的空数组。 variant因此没有价值currentVariant是一个默认情况下等于 0 的数字。”。 How do you expect something to be displayed if your array is empty.如果您的数组为空,您如何期望显示某些内容。 An ngFor is an iteration...一个ngFor是一个迭代......

Hope it helps you, Thierry希望对你有帮助,蒂埃里

In case anyone is looking to add a specific class based on the index, you can do something like this.如果有人希望根据索引添加特定类,您可以执行以下操作。 I personally prefer the syntax here too.我个人也更喜欢这里的语法。 Let's say you wanted to add last-para to the last paragraph from an array:假设您想将last-para添加到数组的最后一段:

<p
  *ngFor="let p of paragraphs; let i = index"
  [class.last-para]="i >= paragraphs.length - 1">
  {{p}}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM