简体   繁体   中英

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 ).

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. You should use jQuery to modify the HTML elements instead.

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

    $modal.modal();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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