Thursday, October 14, 2021

How to use ternary or conditional operator in LWC html Salesforce

This post explains how to use conditional(ternary) operators in LWC salesforce.


Template expression doesn't allow Conditional Expressions. You can not use if-else conditions directly in LWC html. There are specific tags in LWC to use conditional rendering.
  • <template if:true>
  • <template if:false>

use ternary or conditional operator in LWC html Salesforce


ternaryOpt.cmp :


<template>
    <div style = "background-color:wheat; padding: 20px;">
        <lightning-button label="Change" onclick={Change}></lightning-button>
        <div>
        <template if:true={ConditionalCheck}>
                    Hurry!! button clicked :)
            </template>
            <template if:false={ConditionalCheck}>
                    Button is not clicked yet :(
            </template>
        </div>
    </div>
</template>


ternaryOpt.js :


import { LightningElement,track } from 'lwc';
export default class TernaryOpt extends LightningElement {
    @track ConditionalCheck = false;
    Change(){
        this.ConditionalCheck = true;
    }
}


Thanks, 
Lovesalesforceyes

No comments:

Post a Comment

Get selected radio button value in LWC

This post explains how to get selected radio button in LWC. Below is demo of output : To use the radio button in LWC you have to use the  li...