Senin, 18 Maret 2019

How to pass variable from phtml and how to get variable in require js file





















2






























How to pass parameter form .phtml to require js file



Template file



<script type="text/javascript">

require(['Namespace_Module/js/custom'], function () {

});

</script>



JS file



define([

'jquery'

], function ($) {



//here need to get parameter



});

















share|improve this question


















New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.









































  • if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



    – Chirag Patel

    1 hour ago






























2






























How to pass parameter form .phtml to require js file



Template file



<script type="text/javascript">

require(['Namespace_Module/js/custom'], function () {

});

</script>



JS file



define([

'jquery'

], function ($) {



//here need to get parameter



});

















share|improve this question


















New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.









































  • if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



    – Chirag Patel

    1 hour ago


























2






















2














2












How to pass parameter form .phtml to require js file



Template file



<script type="text/javascript">

require(['Namespace_Module/js/custom'], function () {

});

</script>



JS file



define([

'jquery'

], function ($) {



//here need to get parameter



});

















share|improve this question


















New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.






















How to pass parameter form .phtml to require js file



Template file



<script type="text/javascript">

require(['Namespace_Module/js/custom'], function () {

});

</script>



JS file



define([

'jquery'

], function ($) {



//here need to get parameter



});








magento2 phtml requirejs










share|improve this question


















New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.



















share|improve this question


















New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.















share|improve this question





share|improve this question










edited 1 hour ago













Chirag Patel



2,348423







2,348423











New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.














asked 1 hour ago













Vamsy KrishnaVamsy Krishna



111







111







New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.








New contributor









Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.








Vamsy Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.

Check out our Code of Conduct.























  • if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



    – Chirag Patel

    1 hour ago



































  • if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



    – Chirag Patel

    1 hour ago




























if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



– Chirag Patel

1 hour ago









if my answer is helpful to you then you can accept as answer. so it will be useful to another user. Thanks :)



– Chirag Patel

1 hour ago

















1 Answer

1











active



oldest



votes





































2




























In your phtml file add below code.



<script type="text/x-magento-init">

{

"*": {

"Namespace_Modulename/js/testJs": {

"customVar": "<?php echo 'Your parameter'; ?>"

}

}

}

</script>



In you .JS file add below code.



define([

'jquery',

'mage/url'

], function ($, url) {

'use strict';



return function (config) {



var customVar = config.customVar;

console.log(customVar);

}

});



In above code customVar is equal to you need to pass your Parameter. you can pass multiple parameters to create new variable.



I hope it helps!









share|improve this answer













































    Your Answer















    StackExchange.ready(function() {

    var channelOptions = {

    tags: "".split(" "),

    id: "479"

    };

    initTagRenderer("".split(" "), "".split(" "), channelOptions);



    StackExchange.using("externalEditor", function() {

    // Have to fire editor after snippets, if snippets enabled

    if (StackExchange.settings.snippets.snippetsEnabled) {

    StackExchange.using("snippets", function() {

    createEditor();

    });

    }

    else {

    createEditor();

    }

    });



    function createEditor() {

    StackExchange.prepareEditor({

    heartbeatType: 'answer',

    autoActivateHeartbeat: false,

    convertImagesToLinks: false,

    noModals: true,

    showLowRepImageUploadWarning: true,

    reputationToPostImages: null,

    bindNavPrevention: true,

    postfix: "",

    imageUploader: {

    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",

    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",

    allowUrls: true

    },

    onDemand: true,

    discardSelector: ".discard-answer"

    ,immediatelyShowMarkdownHelp:true

    });





    }

    });













    Vamsy Krishna is a new contributor. Be nice, and check out our Code of Conduct.




















    draft saved


    draft discarded



































    StackExchange.ready(

    function () {

    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266323%2fhow-to-pass-variable-from-phtml-and-how-to-get-variable-in-require-js-file%23new-answer', 'question_page');

    }

    );



    Post as a guest




























    Required, but never shown














































    1 Answer

    1











    active



    oldest



    votes















    1 Answer

    1











    active



    oldest



    votes

















    active



    oldest



    votes











    active



    oldest



    votes

















    2




























    In your phtml file add below code.



    <script type="text/x-magento-init">

    {

    "*": {

    "Namespace_Modulename/js/testJs": {

    "customVar": "<?php echo 'Your parameter'; ?>"

    }

    }

    }

    </script>



    In you .JS file add below code.



    define([

    'jquery',

    'mage/url'

    ], function ($, url) {

    'use strict';



    return function (config) {



    var customVar = config.customVar;

    console.log(customVar);

    }

    });



    In above code customVar is equal to you need to pass your Parameter. you can pass multiple parameters to create new variable.



    I hope it helps!









    share|improve this answer





















































      2




























      In your phtml file add below code.



      <script type="text/x-magento-init">

      {

      "*": {

      "Namespace_Modulename/js/testJs": {

      "customVar": "<?php echo 'Your parameter'; ?>"

      }

      }

      }

      </script>



      In you .JS file add below code.



      define([

      'jquery',

      'mage/url'

      ], function ($, url) {

      'use strict';



      return function (config) {



      var customVar = config.customVar;

      console.log(customVar);

      }

      });



      In above code customVar is equal to you need to pass your Parameter. you can pass multiple parameters to create new variable.



      I hope it helps!









      share|improve this answer

















































        2






















        2














        2










        In your phtml file add below code.



        <script type="text/x-magento-init">

        {

        "*": {

        "Namespace_Modulename/js/testJs": {

        "customVar": "<?php echo 'Your parameter'; ?>"

        }

        }

        }

        </script>



        In you .JS file add below code.



        define([

        'jquery',

        'mage/url'

        ], function ($, url) {

        'use strict';



        return function (config) {



        var customVar = config.customVar;

        console.log(customVar);

        }

        });



        In above code customVar is equal to you need to pass your Parameter. you can pass multiple parameters to create new variable.



        I hope it helps!









        share|improve this answer
























        In your phtml file add below code.



        <script type="text/x-magento-init">

        {

        "*": {

        "Namespace_Modulename/js/testJs": {

        "customVar": "<?php echo 'Your parameter'; ?>"

        }

        }

        }

        </script>



        In you .JS file add below code.



        define([

        'jquery',

        'mage/url'

        ], function ($, url) {

        'use strict';



        return function (config) {



        var customVar = config.customVar;

        console.log(customVar);

        }

        });



        In above code customVar is equal to you need to pass your Parameter. you can pass multiple parameters to create new variable.



        I hope it helps!









        share|improve this answer





















        share|improve this answer



        share|improve this answer














        answered 1 hour ago













        Chirag PatelChirag Patel



        2,348423







        2,348423



































            Vamsy Krishna is a new contributor. Be nice, and check out our Code of Conduct.




















            draft saved


            draft discarded

































            Vamsy Krishna is a new contributor. Be nice, and check out our Code of Conduct.
























            Vamsy Krishna is a new contributor. Be nice, and check out our Code of Conduct.




















            Vamsy Krishna is a new contributor. Be nice, and check out our Code of Conduct.























            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            draft saved


            draft discarded



















            StackExchange.ready(

            function () {

            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266323%2fhow-to-pass-variable-from-phtml-and-how-to-get-variable-in-require-js-file%23new-answer', 'question_page');

            }

            );



            Post as a guest




























            Required, but never shown















































































            Required, but never shown
























            Required, but never shown




















            Required, but never shown











            Required, but never shown



















































            Required, but never shown
























            Required, but never shown




















            Required, but never shown











            Required, but never shown









            How to pass variable from phtml and how to get variable in require js file Rating: 4.5 Diposkan Oleh: Admin

            0 komentar:

            Posting Komentar

            Popular Posts