【问题标题】:Is it possible to have a shorthand local variable in ngFor?ngFor 中是否可以有一个简写的局部变量?
【发布时间】:2017-08-11 06:33:02
【问题描述】:

在 Angular 2 ngFor 中,是否可以声明一个本地(速记)变量?我的意思是这样的,但我不确定语法:

<li *ngFor="let userHasAgenda of agendas | async;
    let agenda=userHasAgenda.agenda">

有问题的假设部分:let agenda=userHasAgenda.agenda。 尝试这样的事情,角度给了我: Parser Error: Unexpected token ., expected identifier, keyword, or string at column 43 in [let uha of agendas | async; let agenda=uha.agenda;]

编辑 1: 为了比较,我知道这是可能的,例如,使用 index/first/last:

<li *ngFor="let task of tasks | async;
    let i = index;
    let isFirst = first;
    let isLast = last;"

但是是否允许自定义“别名变量”??

【问题讨论】:

  • 目前,angular 不支持任意赋值。这样做的目的是什么?

标签: angular ngfor


【解决方案1】:

不,根据这个 GitHub issue (Support assigning expression result to local variable within templates),不支持,但有希望:

2016 年 12 月 14 日

可能我们以后可以根据使用需要考虑。

目前,我们必须坚持five NgFor local variables:index、first、last、even 和 odd

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多