将样式设置为仅使用* ngFor创建的第一个元素

三极汉

我正在使用下面的代码来显示一些*字符

  <div *ngFor="let line of lines;let i=index">*</div>

我只想为第一个设置边距。我希望将边距绑定到marginVarcoresponding.ts文件中变量

这就是我为所有元素设置边距的方式。

[style.margin-left.px]="marginVar"

如何将其仅应用于使用创建的第一个元素*ngFor

贡特·佐赫鲍尔(GünterZöchbauer)

ngFor 提供 first

<div *ngFor="let line of lines;let i=index; first as isFirst"
    [style.margin-left.px]="isFirst ? marginVar : 0">*</div>

或使用CSS

<div *ngFor="let line of lines;let i=index; first as isFirst"
    [class.first]="isFirst">*</div>

有关所有*ngFor变量,请参见

https://angular.io/api/common/NgForOf#local-variables

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章