如何在自定义模板中访问Typeahead - Bloodhound建议对象属性

时间:2015-01-05 21:34:27

标签: angularjs typeahead.js typeahead underscore.js-templating bloodhound

使用Typeahead-Bloodhound捆绑包,我创建了一个自定义建议模板。我试图弄清楚模板调用什么对象来编译变量。我使用的是Underscore,而不是{{}}模板:

_.compile( '<p><strong><%=PN%></strong> <span class="tt-suggestion-etc"> <%=A2 %>| LAT: <%=Y%> LON: <%=X%></span></p>' )

如果这是标准的Typeahead括号:

compile( '<p><strong>{{PN}}</strong> <span class="tt-suggestion-etc"> {{A2}}| LAT: {{Y}} LON: {{X}}</span></p>' )

我希望能够通过Underscore中的javascript访问实际的对象属性,如下所示:

_.compile( '<p><strong><%=PN%></strong> <span class="tt-suggestion-etc"> <% if(someObj.someProp !== undefined ){ // do something }else{ //do something else } %>| LAT: <%=Y%> LON: <%=X%></span></p>')

1 个答案:

答案 0 :(得分:0)

在模板中使用console.log(this)后,我挖得足够深,发现答案是“obj”。如在obj.thisProp中,obj.thatProp。所以现在我可以这样做:

_.compile( '<p><strong><%=PN%></strong> <span class="tt-suggestion-etc"> <% if(obj.someProp !== undefined ){ // do something }else{ //do something else } %>| LAT: <%=Y%> LON: <%=X%></span></p>')

希望这有助于其他人寻找答案!