使用Blaze有条件地将复选框标记为在Meteor中已选中 [英] Conditionally mark a checkbox as checked in Meteor using Blaze

查看:61
本文介绍了使用Blaze有条件地将复选框标记为在Meteor中已选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不完全确定这里出了什么问题,或者我应该如何以不同的方式处理它-但是,谷歌搜索只是带来了对"isChecked"的引用-我没有使用过.

I'm not entirely sure what's wrong here, or how I'm supposed to approach it differently - but googling just brings up references to 'isChecked' - something I'm not using.

我只想标记一个复选框是否已选中,如果存在关联的字段值:

I just want to flag a checkbox as checked if the associated field value is present:

<input type="checkbox" name="services.bananaExports" {{ #if currentUser.profile.services.bananaExports }} checked {{ /if }} />

但是我得到了:

反应性HTML属性必须具有常量名称 或由单个{{helper}}组成,提供名称和名称的字典 价值观.此处不允许使用BLOCKOPEN类型的模板标签.

Reactive HTML attributes must either have a constant name or consist of a single {{helper}} providing a dictionary of names and values. A template tag of type BLOCKOPEN is not allowed here.

老实说,我不确定这是要告诉我什么,我是新来的流星/烈火,但这是一个非常基本的if子句?我可以在这里不发表if陈述吗?如果没有,我将如何处理?

Honestly I'm not sure what that's trying to tell me, I'm new to meteor/blaze but it's a pretty basic if clause? Can I not issue an if statement here? If not how would I approach this?

推荐答案

我能想到的最好的解决方案是定义一个这样的助手:

The best solution I can think of is to define a helper like this one:

Template.registerHelper('isTruthy', function(varName) {
  return !!varName;
});

然后您可以执行以下操作:

Then you can do things like this:

{{#each foodList}}
  {{foodName}} <input type="checkbox" checked={{isTruthy eaten}}>
{{/each}}

当然,就您而言:

<input type="checkbox" name="services.bananaExports" checked={{isTruthy currentUser.profile.services.bananaExports}}>

这篇关于使用Blaze有条件地将复选框标记为在Meteor中已选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆