2014-09-12 41 views
0

我想通过使用#each模板指令来显示存储在数组中的键值对(从Session-JSON变量派生)。如何访问(如果可能)访问数组中的对象的字段。使用模板从数组中显示键值对

对不起,如果这是一个已经回答的问题,但我在这里找不到合适的答案。

下面是一些示例代码(模板辅助的一部分):

attributes: function() { 
     var attributes = []; 
     attributes = [{key: test1, value: 1}, {key: test3, value: 2}, {key: test3, value: 3}]; 
     return attributes; 
    }, 

在模板中,我使用的,“这个”或“this.key”。两人都没有像预期的那样工作。

感谢您的任何提示!

回答

0

您是否定义了变量test1test3?它看起来像你把test1test3没有”,所以这意味着JS试图找到这样的名字变量这就是为什么你不能看到this.key工作

var attributes = []; 
attributes = [{key: "test1", value: 1}, {key: "test2", value: 2}, {key: "test3", value: 3}]; 
return attributes; 

模板:。

{{#each attributes}} 
    {{this.key}} : {{this.value}}<br> 
{{/each}} 

输出:

test1 : 1<br> 
test2 : 2<br> 
test3 : 3<br> 

Check here

+0

太棒了,有时你会忘记最简单的事情......!非常感谢! – jacques 2014-09-12 21:11:00