【问题标题】:String interpolation inside ngFor (click)="..."ngFor (click)="..." 中的字符串插值
【发布时间】:2018-05-12 00:40:00
【问题描述】:

像这样在 ngFor 中循环遍历数组:

<div *ngFor="account of accounts">
    <button (click)="function1(); account.message='How do I interpolate {{account.something}} here'"
</div>

我收到错误Got interpolation ({{}}) where expression was expected 如果我只是在没有插值的情况下执行account.something,因为它位于单引号之间,它将打印为文字How do I interpolate account.something

【问题讨论】:

  • 我通常发现最好在绑定中包含尽可能少的逻辑和代码。我建议在你的模块中放置一个单独的处理程序,并让点击处理程序读取(click)="function2(account)"

标签: javascript angular angular5 string-interpolation


【解决方案1】:

只需使用字符串连接,例如:

(click)="account.message='How do I interpolate ' + account.something + ' here'"

【讨论】:

  • 好吧,它起作用了......我以为我已经尝试过了,但它并没有大声笑。谢谢
【解决方案2】:

您已经在双引号内编写了 javascript,因此您无需编写插值语法。

(click)="All javascript code can be written here without interpolation syntax"

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2021-04-23
    • 2012-12-08
    • 2013-03-24
    • 2011-11-15
    • 2020-05-24
    相关资源
    最近更新 更多