Angular 6'da typcript kodundan css'ye nasıl değer iletilir

Dec 02 2020

Ben kullanmayı ettik biliyorum ngClassya ngStyleama bu değer geçmek anlamaya muktedir değilim. İşte kodum.

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'de satır sınıfının genişliğine dinamik olarak şu şekilde karar vermeliyim:

strip.component.css

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

Ben kullanmış olabilir col-{{widthValue}}HTML içinde ama hayır, o css den genişlik özelliğinden olması gerektiğini söylendi. Lütfen bana yardım et. Burada ngClass'ı nasıl kullanacağımdan emin değilim.

Yanıtlar

1 Vishnudev Dec 02 2020 at 18:13

Kullanım class

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

VEYA

Kullanım style.width

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

DEMO