如何使用变量而不是硬编码值访问车把模板内的数组元素?我需要做类似的事情:
{{#each condition in conditions}}
{{App.ops.[condition.op].name}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
此刻并没有给我一个解析错误,但在运行时并没有给我任何回报.如果我做这样的事情:
{{App.ops.[1].name}}
Run Code Online (Sandbox Code Playgroud)
它有效,但它不是我想要的
nic*_*aef 13
您可以使用内置lookup
帮助器:
该
lookup
助手允许使用把手变量的动态参数的分辨率.这对于解析数组索引的值很有用.
使用lookup
,你的例子可以写成
{{#each condition in conditions}}
{{#with (lookup ../App.ops condition.op)}}
{{name}}
{{/with}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)
(请注意,在不了解数据结构的情况下,我会对位置做出假设App.ops
.)