【发布时间】:2018-12-02 20:06:47
【问题描述】:
我正在构建一个常见问题解答部分,该部分通过一个 ngFor 循环和一个常见问题解答项目组件填充常见问题解答。我希望能够使用锚标记链接到页面内的位置。为此,我从 npmjs 下载了 scrollTo 模块。 https://www.npmjs.com/package/ng2-scroll-to
此 scrollTo 模块采用 href 参数,我试图在模板表达式中连接 # 和 ngFor 的索引计数器变量,但是当我这样做时出现错误。这是我的代码。
<section id="top">
<ol>
<li *ngFor="let item of faqItems; let i=index" scrollTo href={{"#" + i}}>{{item.question}}</li>
</ol>
<dl>
<app-faq-item *ngFor="let item of faqItems; let i=index"
[question]="item.question" [answer]="item.answer" [attr.id]="i"></app-faq-item>
</dl>
<button scrollTo scrollableElementSelector="#top" scrollYTarget="0">Go top</button>
</section>
错误来自 href 部分。有人知道如何将 i 变量和 hashsymbol 加入一个字符串吗?
【问题讨论】:
-
试试这个
href="{{'#' + i}}"
标签: angular angular2-template ngfor