Search code examples
nativescriptnativescript-angular

How to apply background-color property in NgClass in nativescript?


If i give class with background color property but when i selected that layout color not change.

enter code here
<StackLayout verticalAlignment="middle"   [ngClass]="{'iconLayout':  item.selected}" class="backWhiteSelect"   (tap)="drinkSelected(i)"></stackLayout>

CSS:
    .iconLayout{
        margin: 5;  
        height: 90;
        width: 185;
        text-align: center;
        border-radius: 7;
        background-color: rgba(112, 112, 112, 0.15);
    }

Solution

  • add new StackLayout with ngClass

    <ng-template let-item="item" let-i="index">
            <StackLayout verticalAlignment="middle">
                <StackLayout [ngClass]="item?.selected ? 'backWhiteSelect' : 'iconLayout'" (tap)="onTap(item)">
                    <Label [text]="item?.name" class="text-center gray-66 h3"></Label>
                </StackLayout>
            </StackLayout>
        </ng-template>
    

    tap event in componenet

    onTap(item) {
        this.interestsItems.forEach(m => m.selected = false);
        item.selected = true;
    }