Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a shorthand local variable in ngFor?

Tags:

angular

ngfor

In Angular 2 ngFor, is it possible to declare a local (short-hand) variable? I mean something like this, but I am not sure about the syntax:

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

The hypothetical part in question: let agenda=userHasAgenda.agenda. Trying something like that, angular gives me: Parser Error: Unexpected token ., expected identifier, keyword, or string at column 43 in [let uha of agendas | async; let agenda=uha.agenda;]

Edit 1: For comparison, I know this is possible, for example, with index/first/last:

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

But is it allowed to have custom "aliased variables" ??

like image 309
KarolDepka Avatar asked Mar 20 '17 04:03

KarolDepka


1 Answers

No, according to this GitHub issue (Support assigning expression result to local variable within templates), it isn't supported, but there is hope:

14 Dec 2016

Probably something we can consider at a later point according to usage needs.

For now, we have to stick to the five NgFor local variables: index, first, last, even and odd

like image 65
Christopher Moore Avatar answered Oct 26 '22 22:10

Christopher Moore