简体   繁体   中英

pass object array to handlebars

I have an associative array. I need to pass it to the handlebars template Below is my code.

Javascript

    var arr1 = new Array();
        arr1['title'] = "title1";
        arr1['subt'] = "subtitle";

    getTemplate('popupTemplate1.html', arr1).done(function(data){
        $('#Data_popup').find('.popContent').html(data);
        $('#Data_popup').fadeIn(1000);
    })

function getTemplate( name,data){
  var d=$.Deferred();

  $.get(name,function(response){

    var template = Handlebars.compile(response);
    d.resolve(template(data))
  });

  return d.promise();  
}

Template structure is

<ul>
    <li>{{arr1.title}}</li>
    <li>{{arr1.subt}}</li>
</ul>

But this is not working. There is no output and if I check length of arr1, it gives me 0 I'm not getting how to reference array inside template What I'm doing wrong here?

Thank you!

Thank you for the reply but I able to sort this issue. I change the declaration of array to object as below,

var arr1 = new Object();

This way, now I'm able to access this object inside handlebar template as below,

<ul>
    <li>{{title}}</li>
    <li>{{subt}}</li>
</ul>

Anyways, thank you for the explanation.

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