简体   繁体   English

如何将变量传递给车把助手?

[英]How do I pass a variable to a handlebars helper?

I'm trying to implement a simple "if-equal" helper in handlebars, like so: 我试图在把手中实现一个简单的“if-equal”帮助器,如下所示:

Ember.Handlebars.registerHelper('ifeq', function(val1, val2, options) {
    return (val1 == val2) ? options.fn(this) : '';
});

And use it like so (let's assume the foo variable is set to "bar"): 并使用它(假设foo变量设置为“bar”):

{{#ifequal foo "bar"}} show something {{/ifequal}}

The problem is, when val1 is passed in, it's passed in as the string "foo", and not the actual value of the foo variable. 问题是,当传入val1时,它作为字符串“foo”传入,而不是foo变量的实际值。 In my debugger, I can verify that this[val1] is in fact set to the expected value of the foo variable. 在我的调试器中,我可以验证这个[val1]实际上是设置为foo变量的预期值。

Does Ember somehow alter the behavior? Ember会以某种方式改变这种行为吗?

Ember.Handlebars.registerHelper just passes strings in. There is a registerBoundHelper being worked on, but in your case this should work. Ember.Handlebars.registerHelper只是传入字符串。有一个registerBoundHelper正在处理,但在你的情况下这应该工作。

Ember.Handlebars.registerHelper('ifeq', function(val1, val2, options) {
   return (this.get(val1) == val2) ? options.fn(this) : '';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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