Posted on / Magento2

How to set ‘if’, ‘else’, ‘and’ conditions in knockout js Magento 2?

Share the content

This post will give you the idea in certain ways to use if else, and condition in knockout JS in Magento 2

If else conditions in KO template

<!-- ko if: myData -->

..if statement goes here

<!-- /ko -->
<!-- ko if: !(myData) -->

..else statement goes here

<!-- /ko -->

 

If else conditions in KO template with && operator

<!-- ko if: X && Y -->

    //your code
<!-- /ko -->

<!-- ko if: X && !Y -->

    //your code
<!-- /ko -->

<!-- ko if: !X && !Y -->

    //your code
<!-- /ko -->

Leave a Reply

Your email address will not be published. Required fields are marked *