如何循环遍历模板中的数组?
How do I loop through an array in a template?
我正在项目中使用Angular's schematics。
我有一个数字数组:const numbers = [1, 2, 3, 4, 5]
我想在 HTML 模板中打印出来。我通过以下方式传递数组:
export function schematics(_options: Schema): Rule {
return (tree: Tree, _context: SchematicContext) => {
const numbers = [1, 2, 3, 4, 5];
const sourceTemplate = url('./files');
const sourceParametrizedTemplate = apply(sourceTemplate, [
template({
..._options,
...strings,
numbers
})
]);
return mergeWith(sourceParametrizedTemplate)(tree, _context);
};
}
我正在考虑做这样的事情:
index.html
<h1>Numbers</h1>
<%= for(let n in numbers) { =>
<p><%= n %></p>
<%= } %>
但是我收到错误 SyntaxError: Unexpected token for
。
有谁知道实现它的正确方法是什么?我无法在文档中找到它。
谢谢!
终于找到了!我不得不在逻辑块中使用 <%
而不是 <%=
。
<%=
应仅用于您要显示的块。
最终版本:
<h1>Numbers</h1>
<% for(let n in numbers) { =>
<p><%= n %></p>
<% } %>
顺便说一句,我正在寻找的结果是使用 for ... of ...
而不是 for ... in ...
得到的,但它们都有效。
我正在项目中使用Angular's schematics。
我有一个数字数组:const numbers = [1, 2, 3, 4, 5]
我想在 HTML 模板中打印出来。我通过以下方式传递数组:
export function schematics(_options: Schema): Rule {
return (tree: Tree, _context: SchematicContext) => {
const numbers = [1, 2, 3, 4, 5];
const sourceTemplate = url('./files');
const sourceParametrizedTemplate = apply(sourceTemplate, [
template({
..._options,
...strings,
numbers
})
]);
return mergeWith(sourceParametrizedTemplate)(tree, _context);
};
}
我正在考虑做这样的事情:
index.html
<h1>Numbers</h1>
<%= for(let n in numbers) { =>
<p><%= n %></p>
<%= } %>
但是我收到错误 SyntaxError: Unexpected token for
。
有谁知道实现它的正确方法是什么?我无法在文档中找到它。
谢谢!
终于找到了!我不得不在逻辑块中使用 <%
而不是 <%=
。
<%=
应仅用于您要显示的块。
最终版本:
<h1>Numbers</h1>
<% for(let n in numbers) { =>
<p><%= n %></p>
<% } %>
顺便说一句,我正在寻找的结果是使用 for ... of ...
而不是 for ... in ...
得到的,但它们都有效。