我正在使用以下代码来显示某些数量的*
字符
<div *ngFor="let line of lines;let i=index">*</div>
我希望只给第一个设置边距。我想将边距绑定到相应的
.ts
文件中的marginVar
变量中。这是如何将边距设置为所有元素的方法。
[style.margin-left.px]="marginVar"
我该如何将其应用到仅由
*ngFor
创建的第一个元素?我正在使用以下代码来显示某些数量的*
字符
<div *ngFor="let line of lines;let i=index">*</div>
.ts
文件中的marginVar
变量中。[style.margin-left.px]="marginVar"
*ngFor
创建的第一个元素?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
变量,请参见:
[class.first]="i === 0"
ngIf
(ngIfThen
/ngIfElse
)。