[英]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.