Calling typeof operator in jQuery Template data tags?
Is it possible to call typeof(x)
within a data rendering template tag?
${typeof(x)}
Obligatory example jsFiddle
Workaround
I can alias the typeof
operator as a custom template function and it works fine, but I would like to avoid that extra level of redirec开发者_如何学Gotion if I can.
// calling JavaScript
$("#reflectTemplate").tmpl(data, {
getType: function(itemToCheck) {
return typeof(itemToCheck);
}
}).appendTo(".results");
<!--template tag-->
${$item.getType(prop)}
What you want is to force the result to be a string. One or both of these should work in most browsers:
${String(typeof(prop))}
${typeof(prop)+''}
精彩评论