#流星雨中数组中的每个字符串 [英] #each string in an array with blaze in Meteor

查看:69
本文介绍了#流星雨中数组中的每个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表中呈现了一组用户名,如下所示:

I have an array of usernames that I am rendering in a list like so:

{{#each contacts}}
<div class="name">{{this}}</div>
{{/each}}

这工作正常,但随后我尝试从事件中获取用户名:

This works just fine, but then I try to get the username from an event:

'click .name': function(e,t){
console.log(this)
}

我得到了这个令人沮丧的对象String {0: "c", 1: "h", 2: "a", 3: "r", 4: "l", 5: "i", 6: "e", length: 7, [[PrimitiveValue]]: "charlie"},这使得与之进行字符串比较非常具有挑战性.为什么这甚至是一个问题或如何解决的任何想法?

I get this frustrating object String {0: "c", 1: "h", 2: "a", 3: "r", 4: "l", 5: "i", 6: "e", length: 7, [[PrimitiveValue]]: "charlie"} which makes it very challenging to do string comparisons with. Any ideas why this is even a problem or what to do about it?

推荐答案

通常在Javascript中,上下文必须是对象而不是原始对象(

In general in Javascript, context has to be an object rather than a primitive (link). Presumably, contacts is just an array of strings, so within each div tag these strings are boxed (i.e. cast to a reference type, in this case the String object). That's what you're logging here - the String object, rather than your original primitive.

您有两个选择:

  1. 如果使用this.valueOf(),它将返回原始字符串.
  2. 或者,考虑将接触点设置为对象数组(例如[{value: 'nameOne'}, ...]).这样,您可以在事件处理程序中将{{this}}替换为{{value}}this,将以您提供的相同格式将对象退还给您.
  1. if you use this.valueOf() it will give you the primitive string back.
  2. alternatively, consider making contacts an array of objects (like [{value: 'nameOne'}, ...]). That way, you can replace {{this}} with {{value}} and this in your event handler will give you back the object in the same format you supplied it.

这篇关于#流星雨中数组中的每个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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