简体   繁体   English

直接在 Handlebars 模板中添加数组长度的 if 条件

[英]Add if condition on array length directly in Handlebars template

In my Handlebars template, I want to enable/disable a button based on "selectedRows" array length (ie enable if selectedRows > 0 and disable it otherwise ie on array length 0) I want to avoid introducing any extra instance variable in the component JS.在我的 Handlebars 模板中,我想启用/禁用基于“selectedRows”数组长度的按钮(即,如果 selectedRows > 0 则启用,否则禁用它,即数组长度为 0)我想避免在组件 JS 中引入任何额外的实例变量. Is it possible to do this directly in handlebars?是否可以直接在车把中执行此操作?

ie something like below即类似下面的东西

{{my-button count=selectedRows enabled=(if selectedRows > 0)}}

enabled is boolean (true/.false)启用是 boolean (true/.false)

If you install the widely-used Ember Truth Helpers addon , you could have something like this:如果你安装了广泛使用的Ember Truth Helpers 插件,你可能会有这样的东西:

{{my-button count=selectedRows enabled=(gt selectedRows 0)}}

If your requirement is just checking if length is greater than 0 , you could do, (assuming selectedRows is a Number ),如果您的要求只是检查长度是否大于0 ,您可以这样做(假设selectedRowsNumber ),

{{my-button count=selectedRows enabled=(if selectedRows true false)}}

Since JS considers 0 a falsy value, you could use it as the condition.由于 JS 认为 0 是一个虚假值,因此您可以将其用作条件。

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

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