繁体   English   中英

无法将值传递给车把助手

[英]Failed to pass value to handlebars helper

我想比较模板中的两个值,因此我使用了如下给出的把手助手:

Handlebars.registerHelper('ifCond', function(v1, v2, options) {
    console.log(v1);console.log(v2);
  if(v1 === v2) {
    return options.fn(this);
  }
  return options.inverse(this);
});

我的模板代码:

{{#ifCond item.id 1}}                           
    <div class="row" style="margin-top:0px;">                           
{{else}}
    <div class="row" style="margin-top:5px;">
{{/ifCond}} 

但是item.id值没有传递给helper,它将其值显示为“item.id”。

谁能告诉我上面代码中的错误是什么?

我不建议使用if语句在标记之间进行更改。 我自己做了,这是一个脆弱的方法,不是很简洁。 我总是会尝试使用帮助器{{bind-attr}} (帮助程序在rc7及更低版本中称为{{bindAttr}})。

CSS:

row.margin-top{
  margin-top: 5px;
}

模板:

<div class="row" {{bindAttr class="item.isWithMargin:margin-top"}}>

这不是最优雅的方法,因为您的模型中会有css特定的逻辑。 因此,您可以迭代您的项目并为每个项目分配itemController并将逻辑放在此控制器中。

{{#each item in controller itemController="yourItemController"}}
<!-- this would resolve to App.YourItemController -->
App.YourItemController = Ember.ObjectController({
  isWithMargin : function(){
    return this.get("model.id") == 1;
  }.property("model.id")
});

暂无
暂无

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

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