简体   繁体   English

玉变量渲染

[英]Jade variable rendering

Folks, How would I access variables that I set in script. 伙计们,我如何访问我在脚本中设置的变量。 ?

#someModal.modal.fade.large
 .modal-dialog(style="width:70%")
    .modal-content
        .modal-header
            button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
            h2.modal-title #{someVariable}
        .modal-body
            h3
               ...

script.
    | var someVariable = #{someVariable};

script.
    $('body').on('show.bs.modal', '.modal', function () {
        var someVariable = 'test';
    });

The workaround I typically use is just attaching the variable to the window: 我通常使用的解决方法是将变量附加到窗口:

#someModal.modal.fade.large
 .modal-dialog(style="width:70%")
    .modal-content
        .modal-header
            button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
            h2.modal-title #{someVariable}
        .modal-body
            h3
               ...
script
  | var someVariable = #{someVariable};
script.
    $('body').on('show.bs.modal', '.modal', function () {
        var someVariable = 'test';
    });

Note that you'll need to JSON.stringify the variable if it is an Object (or Array ). 请注意,如果变量是Object (或Array ),则需要对变量进行JSON.stringify

Your script will not start to run until your page has loaded, so jade will never have access to the variables that are set on the client. 在您的页面加载之前,您的脚本将不会开始运行,因此jade永远无法访问客户端上设置的变量。 You should use jQuery to modify the HTML elements instead. 您应该使用jQuery来修改HTML元素。

script.
    var $modal = $('#someModal')
      , $title = $modal.find('.modal-title');
    $modal.on('show.bs.modal', '.modal', function () {
        var someVariable = 'test';
        $title.text(someVariable);
    });

    $modal.modal();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM