如何比较车把中的值? [英] How to compare a value in handlebars?
本文介绍了如何比较车把中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据条件显示不同的HTML.
I want display different HTML depending on a condition.
似乎没有将这两个值进行比较,并且总是显示第一个变体.如何将预定义值与JSON中的原始值进行比较,以使其能够正确执行?
It doesn't seem to compare those two values and it always shows the first variant. How can I compare the predefined values to the original value from JSON so that it can execute properly?
{{#each this}}
{{#each visits}}
<div class="row">
{{#if variable_from_json }}
<div class="col-lg-2 col-md-2 col-sm-2">
<i class="fa fa-home"></i>
</div>
{{else}}
<div class="col-lg-2 col-md-2 col-sm-2">
<i class="fa fa-plus symbol-hospital"></i>
</div>
{{/if}}
</div>
{{/each}}
{{/each}}
JS代码
Handlebars.registerHelper('if', function (variable_from_json, options) {
if (variable_from_json === "M") {
return options.fn(this);
} else {
return options.inverse(this);
}
});
推荐答案
要比较==,> =,||之类的值,&& 创建一个可以处理所有情况的助手
To compare values like ==,>=,|| ,&& Create one helper which will handle all cases
Handlebars.registerHelper( "when",function(operand_1, operator, operand_2, options) {
var operators = {
'eq': function(l,r) { return l == r; },
'noteq': function(l,r) { return l != r; },
'gt': function(l,r) { return Number(l) > Number(r); },
'or': function(l,r) { return l || r; },
'and': function(l,r) { return l && r; },
'%': function(l,r) { return (l % r) === 0; }
}
, result = operators[operator](operand_1,operand_2);
if (result) return options.fn(this);
else return options.inverse(this);
});
在车把文件中使用此运算符,例如. ==运算符
Use this operator in handlebar file For eg. == operator
{{#when <operand1> 'eq' <operand2>}}
// do something here
{{/when}}
这篇关于如何比较车把中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文