Magento – Magento2, define variable in html (Knockout) file

htmlknockoutjsmagento2

Consider below code, the condition ko if: falg does not work. How to define variable falg = "false" in this file?

<!-- ko falg = "false" --> <!-- /ko -->
<!-- ko if: product_has_url -->
    <!-- ko falg = "true" --> <!-- /ko -->
<!-- /ko --> 
<!-- ko if: falg -->
...
<!-- /ko --> 

update: I'm writing module to customize the minicart content. Below is the structure:

   registration.php
    |   
    +---etc
    |   |   module.xml
    |   |   
    |   \---frontend
    |           di.xml
    |           
    +---Plugin
    |       DefaultItem.php
    |       
    \---view
        +---frontend
            \---layout
            |     checkout_cart_sidebar_item_renderers.xml
            |       
            \---web
              \---js
                 \---myscript.js
              \---template
                 \---minicart
                    \---item
                            default.html 
    \---requirejs-config.js

So now my challenge is to pass the flag from myscript.js to based on the item attibute product_has_url to default.html

myscript.js ?

define([
    'uiComponent',
    'Magento_Customer/js/customer-data',
    'jquery',
    'ko',
    'sidebar'
], function (Component, customerData, $, ko) {


    function checkAttr() {


    }

    return Component.extend({

        }); 

});

Best Answer

You can try this :

<!-- ko if: falg -->
...
...
<!-- /ko --> 
<!-- ko ifnot: falg -->
...
...
<!-- /ko --> 

In js file you can use like this :

 return Component.extend({.....}),
    initObservable: function () {
        var me = this;
        me.flag(true);
        return this;
    }, ....

After this you need to deploy static content or to test you can add changes directly in the same js and html file of pub/static.

Related Topic