Как передать значение из кода машинописного текста в CSS в Angular 6

Dec 02 2020

Я знаю, что должен использовать 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 примерно так:

strip.component.css

.rows {
    display: inline-block;
    width: calc(100%/widthValue); // <----- here
    text-align: left !important;
    background: red;
}

Я мог бы использовать col-{{widthValue}}в HTML, но нет, мне сказали, что это должно быть из свойства width из css. Пожалуйста, помогите мне. Я не уверен, как использовать здесь ngClass.

Ответы

1 Vishnudev Dec 02 2020 at 18:13

Использовать class

<div [class]="'col-'+ widthValue"></div>

ИЛИ ЖЕ

Использовать style.width

<div [style.width]="(widthValue/12)*100 + '%'"></div>

ДЕМО