在Spacebars中检查是否相等?

编程入门 行业动态 更新时间:2024-10-19 02:19:01
本文介绍了在Spacebars中检查是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试做我认为应该是一项非常简单的任务,但过去一小时内没有这样做。如果用户属性与值匹配,我想默认选择一个选项。

I am trying to do what I think should be a very simple task, but have been failing to do so in the past hour. I want to select a select option by default if the user property matches the value.

<select name="myName"> {{#each addKeys myTable}} <!-- addKeys creates variables for keys and values --> <option value="{{key}}" {{#if currentUser.property === key}}selected="selected"{{/if}}>{{value}}</option> {{/each}} </select>

现在我认为这很简单,可以实施。但事实证明,Spacebars不允许除否定感叹号之外的条件运算符,因此等号无关紧要。然后我尝试了一些可怕的尝试:

Now I thought this was straightforward enough to be implemented. But it turns out that Spacebars do not allow conditional operators other than the negation exclamation mark, so equal signs are out of question. I then tried something horrible for the sake of trying:

在模板 myTemplate :

<select name="myName"> {{#each addKeys myTable}} <option value="{{key}}" {{isSelected currentUser.property key}}>{{value}}</option> {{/each}} </select>

在 mytemplate.js :

Template.myTemplate.helpers({ isSelected: function(v1, v2) { if (v1 === v2) return "selected=\"selected\""; return ''; } });

此代码不仅可怕,看起来很糟糕,不起作用:

Not only is this code terrible, terrible to look at, it does not work:

Exception in Meteor UI: String contains an invalid character

我不明白为什么这么简单的东西似乎无法实现。我错过了什么?

I don't understand why something that simple seems so impossible to achieve. Am I missing something there?

推荐答案

试试这个:

In您的模板:

< option value = {{key}} selected = {{isSelected currentUser.property key}}>

然后让你的助手返回一个布尔值。

Then have your helper return a boolean.

有关此主题的更多信息: github/meteor/meteor/ wiki / Using-Blaze#conditional-attributes-with-no-value-eg-checked-selected

More on the topic here: github/meteor/meteor/wiki/Using-Blaze#conditional-attributes-with-no-value-eg-checked-selected

更多推荐

在Spacebars中检查是否相等?

本文发布于:2023-11-29 17:08:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spacebars

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!