Jak przekazać wartość z kodu maszynopisu do css w Angular 6

Dec 02 2020

Wiem, że muszę użyć ngClasslub, ngStyleale nie jestem w stanie dowiedzieć się, jak przekazać tę wartość. Oto mój kod.

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>

Muszę dynamicznie decydować o szerokości klas wierszy w scss, coś takiego:

strip.component.css

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

Mogłem użyć col-{{widthValue}}w HTML, ale nie, powiedziano mi, że musi to pochodzić z właściwości width z css. Proszę pomóż mi. Nie jestem pewien, jak używać ngClass tutaj.

Odpowiedzi

1 Vishnudev Dec 02 2020 at 18:13

Posługiwać się class

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

LUB

Posługiwać się style.width

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

PRÓBNY