车把自定义帮助程序错误:“options.fn 不是函数”
Handlebars custom helper error: "options.fn is not a function"
我对车把很陌生.js我尝试编写一个简单的 if/else 助手。
我用这个代码笔作为指导方针。
我已经发现您不应该在自定义帮助程序前面使用该#
,但我无法弄清楚为什么我仍然收到此错误。
这是我index.html
:
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js">
</script>
<script type="text/javascript" src="
https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<script src="data.js"></script>
<script src="helper.js"></script>
</head>
<body>
<script id="entry-template" type="text/x-handlebars-template">
<ul class="test">
{{#each people}}
{{ifThird @index}}
<li>
{{firstName}}
</li>
{{else}}
<li>
{{firstName}}{{lastName}}
</li>
{{/each}}
</ul>
</div>
</div>
</script>
</body>
</html>
。这是data.js
:
$(function(){
var templateScript = $("#entry-template").html();
var theTemplate = Handlebars.compile(templateScript);
var context = {
people: [
{firstName: "Yehuda", lastName: "Katz"},
{firstName: "Carl", lastName: "Lerche"},
{firstName: "Alan", lastName: "Johnson"},
{firstName: "Dik", lastName:"Neok"},
{firstName: "Bob", lastName:"van Dam"},
{firstName: "Erik", lastName: "Leria"}
]
};
var html = theTemplate(context);
$('.test').html(html);
$(document.body).append(html);
})
。这是helper.js
(错误应该在这里):
Handlebars.registerHelper('ifThird', function (index, options) {
if(index%3 == 0){
return options.fn(this);
} else {
return options.inverse(this);
}
});
您需要将模板编辑为:
<ul class="test">
{{#each people}}
{{#ifThird @index}}
<li>
{{firstName}}
</li>
{{else}}
<li>
{{firstName}}{{lastName}}
</li>
{{/ifThird}}
{{/each}}
</ul>
您需要以 {{#ifThird}} 开始块,并使用 {{/ifThird}} 关闭块。
相关文章:
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- 通过fn.apply或fn.bind将函数传递给setTimeout
- 函数 (){ 返回 fn.apply(me, arguments); } 作为回调
- 将函数绑定到baz后调用fn.apply(bar,[])
- 如何在其他函数中使用fn作为函数参数
- angularjs[ng:areq]参数'fn'不是函数,在控制台中获取字符串时出错
- 在异步内部函数返回 [UI-路由器和 HTML 服务] 之后返回外部 fn
- Javascript:全局变量转到“窗口”或“全局”,但全局函数不会.全球 FN 去哪儿了
- 扩展 $.fn.init 函数
- 未捕获的类型错误:fn 不是图表.js中的函数
- 车把自定义帮助程序错误:“options.fn 不是函数”
- D3 强制布局 Tick Fn - nodes.attr() 不是一个函数
- 为什么<车身负载=“;fn()">使用函数调用,但是window.onload=fn;使用处理程序函
- $routeProvider配置路由抛出'未捕获错误:[ng:areq]参数'fn'不是函数,得
- 在AngularJS中定义初始应用程序-Argument'fn'不是函数,获取字符串
- 传递给setTimeout的函数fn不是作为函数调用的,而是作为类似window.fn()的方法调用的
- 未捕获的类型错误:对象函数(e,t){return new b.fn.init(e,t,r)}没有方法'feat
- 当我想调用update()函数时,jQuery.fn.yiiGridView抛出未定义错误
- $.fn. yiigridview.getSelection函数不能立即工作
- 参数& # 39;fn # 39;不是一个函数,得到Object angularjs [ng:areq]