Tanzeel
Tanzeel

Reputation: 5038

How to pass value from typescript code to css in Angular 6

I know I've to use ngClass or ngStyle but I'm not able to figure out how to pass that value. Here is my code.

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>

I've to decide width of rows class dynamically in scss something like this:

strip.component.css

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

I could have used col-{{widthValue}} in HTML but no, I've been told that it has to be from the width property from css. Please help me. I'm not sure how to use ngClass here.

Upvotes: 2

Views: 3834

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10970

Use class

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

OR

Use style.width

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

DEMO

Upvotes: 3

Related Questions