如何循环遍历模板中的数组?

Lea*_*dro 1 angular angular-schematics

我在一个项目中使用Angular 的原理图

我有一个数字数组: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);
  };
}
Run Code Online (Sandbox Code Playgroud)

我正在考虑做这样的事情:

索引.html

<h1>Numbers</h1>

  <%= for(let n in numbers) { =>

  <p><%= n %></p>

  <%= } %>
Run Code Online (Sandbox Code Playgroud)

但我得到了错误SyntaxError: Unexpected token for

有谁知道实现它的正确方法是什么?我在文档中找不到它。

谢谢你!

Lea*_*dro 6

我终于找到了!我必须使用<%而不是<%=在具有逻辑的块中。

<%=应该只用于您想要显示的块。

最终版本:

<h1>Numbers</h1>

  <% for(let n in numbers) { =>

  <p><%= n %></p>

  <% } %>
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我正在寻找的结果是我使用而for ... of ...不是得到的for ... in ...,但它们都有效。