Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 流星手柄{{{#if}}将字符串转换为对象_Meteor_Handlebars.js - Fatal编程技术网

Meteor 流星手柄{{{#if}}将字符串转换为对象

Meteor 流星手柄{{{#if}}将字符串转换为对象,meteor,handlebars.js,Meteor,Handlebars.js,假设我使用一个普通的{{{{each}}循环在Meteor中的一个数组上循环。每个数组元素都是一个字符串,我在每一步都使用{{this}输出字符串。到现在为止,一直都还不错!如果我使用车把助手检查typeof中的this我将得到string。好极了!一切都是应该的 但是如果我在{{{each}中添加一个{{{{if something}-helper(something只返回true,并继续运行并输出{this}),字符串在HTML中看起来仍然不错,但现在它是类型的检查中的对象 这非常烦人,因为

假设我使用一个普通的
{{{{each}}
循环在Meteor中的一个数组上循环。每个数组元素都是一个字符串,我在每一步都使用
{{this}
输出字符串。到现在为止,一直都还不错!如果我使用车把助手检查
typeof
中的
this
我将得到
string
。好极了!一切都是应该的

但是如果我在
{{{each}
中添加一个
{{{{if something}
-helper(
something
只返回true,并继续运行并输出
{this}
),字符串在HTML中看起来仍然不错,但现在它是
类型的
检查中的
对象

这非常烦人,因为我的代码现在可能依赖的所有
(someVarINeedToTest==='string')
都将返回false

我做错什么了吗

或者这是一个真正的错误

如果是:是流星特有的还是车把特有的虫子

谢谢


哦:。只需拉动并运行meteor,然后查看浏览器控制台。

这是因为
This
变量在JavaScript中应该始终是一个对象,所以当
someFunction.apply(someContext)时
由Handlebar调用,JavaScript将
someContext
变成一个对象,而不管它是以什么形式开始的。请参见此处的示例:

(在本例中,
someFunction
表示模板中位于
{{{if}}
语句中的部分。)

一个简单(尽管很难看)的解决方法是始终将数据作为对象传递,因此

['this', 'is', 'an', 'array', 'that', 'we\'re', 'looping', 'through'];
变成:

[{val: 'this'}, {val: 'is'}, {val: 'an'}, {val: 'array'}, {val: 'that'}, {val: 'we\'re'}, {val: 'looping'}, {val: 'through'}];

然后你将模板改为查看
val
,而不是
this

干杯,回答得好!在比较字符串时可以使用的另一种解决方法是:u.isEqual(这是StringToCompareAginst)。还要注意,Template.currentData()返回的是字符串而不是对象