繁体   English   中英

TextArea中的For-Each循环

[英]For-Each Loop in TextArea

当我尝试在纯HTML textarea中使用Handlebars ForEach循环时,我遇到了一些意想不到的行为。

首先让我告诉你我的代码:

我的template.html:

<template name="test">
  <textarea>
    {{#each array}}
      {{this}}
    {{/each}}
  </textarea>
</template>

我的template.js:

Template.test.array = function(){
    return ["String1", "String2", "String3"];
}

问题:

我实际上期望一些看起来像这样的输出:

String1 
String2 
String3

相反,我得到了这个:

<!--data:46XrgsL9aX8aCa3Ek-->
<!--data:tu6FiCxRraSLoh2w9-->
<!--data:5h3PkyB66dHw4zrWF-->

作为一种解决方法,我使用了Handlebars助手并使用jQuery操纵textarea的值,这非常有效。

我还想对这个案子做一些澄清:

  1. 为什么我没有得到预期的输出? 如果我在textarea之外使用循环,它会正确打印字符串。
  2. 这些弦是什么? JavaScript的内部对象ID等?

每一个小帮助都表示赞赏!

这些字符串看起来像Spark地标 这很奇怪,但可能会在下一个Meteor版本中重写渲染引擎时修复。

这可能与字符串文字作为上下文传递给帮助者时的问题有关; 它变成了一个对象。

https://github.com/meteor/meteor/issues/1447

回到字符串文字的一种方法是编写一个帮助器。 而不是{{this}} ,写{{printString}} ,并定义

Template.test.printString = function() { return "" + this; }

嗯,这应该工作! 如果在安德鲁的建议之后仍然没有,请尝试以下方法:

1)将数据移出帮助程序:

var array = ["String1", "String2", "String3"];
Template.test.array = function(){
  return array;
};

2)将数据条目包装在对象中,作为解决方法:

Template.test.array = function(){
  return [{label: "String1"}, {label: "String2"}, {label: "String3"}];
};

{{#each array}}
  {{label}}
{{/each}}

暂无
暂无

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

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