'What is the difference of ".. let i = index" and "..let i as index"?
I am new to angular and I've been watching some youtube videos. Some of them use "let item of items; let i as index" and another one use "let item of items; let i = index". I tried to search for their difference in google, but I can't seem to find a simple answer. Can somebody explain what's the difference of the bolded codes? Thank you!
Solution 1:[1]
It's just shorthand(minified code), for example this code
<li *ngFor="let item of items; index as i; trackBy: trackByFn">...</li>
is shorthand code for this:
<ng-template ngFor let-item [ngForOf]="items" let-i="index" [ngForTrackBy]="trackByFn">
<li>...</li>
</ng-template>
So baiscally the are the same :)
Angular Docs : link
Solution 2:[2]
thank you for answering my question.
I'd also like to clarify some things above. You said it is just a shorthand code of the other. So basically, I tried a few things and here's what I got:
from the codes:
<div class="todo" *ngFor="let list of lists; index as i">
and
<div class="todo" *ngFor="let list of lists; let i = index">
They yield the same result. But from my question, I wrote
<div class="todo" *ngFor="let list of lists; let i as index">
this will yield in an [object]
So, I'll assume that this code:
is just a typo.Thank you for clarifying their difference. Really appreciate the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mostafa Hassan |
| Solution 2 | TxRx21 |
