【发布时间】:2021-03-14 08:01:09
【问题描述】:
我知道我必须使用ngClass 或ngStyle,但我不知道如何传递该值。这是我的代码。
strip.component.ts
import { ... } from '@angular/core';
@Component({
selector: 'app-strip',
templateUrl: './strip.component.html'
})
export class StripComponent implements OnInit {
...
@Input() widthValue: any; // getting this from parent component
staticKpi:{...}=[][]
}
strip.component.html
<ng-container>
<div *ngFor="let row of staticKpi">
<div class="rows" *ngFor="let item of row">
<div class="col-12">
<h2>{{widthValue}}</h2> <!-- this is printing correct value-->
...
</div>
</div>
</div>
</ng-container>
我必须在 scss 中动态决定 rows 类的宽度,如下所示:
strip.component.css
.rows {
display: inline-block;
width: calc(100%/widthValue); // <----- here
text-align: left !important;
background: red;
}
我本可以在 HTML 中使用col-{{widthValue}},但不,我被告知它必须来自 CSS 的 width 属性。
请帮我。我不确定如何在这里使用 ngClass。
【问题讨论】:
-
使用
ngStyle动态设置宽度。 -
你可以在.html中使用:
[style.width]="(100/widthValue)+'%'"或[ngClass]="'col-'+widthValue" -
@Eliseo,这不起作用。元素刚刚相互碰撞,就好像宽度为零一样。 :-(
-
您可以使用任何表达式,例如,例如
[style.width]="widthValue?(100/widthValue)+'%':'1%'"
标签: html css angular typescript