11

Is it possible to create local variable in *ngFor? For example:

<div *ngFor="let user of users; let myVariable = 0">
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Stack Over
  • 287
  • 2
  • 4
  • 14
  • There is no way to create local variables. What's the problem with adding it to the components class? – Günter Zöchbauer Jan 08 '18 at 07:58
  • What is it exactly you're trying to achieve? You can have a local template variable holding the index of the for loop if that's what you're looking for. – Amit Jan 08 '18 at 09:20
  • You can't create local variables but you can have indices of your users, like `*ngFor="let user of users; let i = index"` – Quan VO Jan 08 '18 at 09:30

3 Answers3

13

You can use ng-container with a ngIf to achieve this.

<div class="message-list">
  <ng-container *ngFor="let message of messages">
    <ng-container
      *ngIf="{
        isCurrentUser: message.createdBy.email === currentUser.email
      } as locals"
    >
      <div
        class="message"
        [ngClass]="{ 'message--right-aligned': locals.isCurrentUser }"
      >
        <div>{{ message.text }}</div>
        <div [ngClass]="{ bold: !locals.isCurrentUser }">
          {{ message.createdBy.name }}
        </div>
      </div>
    </ng-container>
  </ng-container>
</div>
tropicana
  • 1,403
  • 2
  • 19
  • 27
5

It's not possible to create a custom local variable within an *ngFor, you can only use the 8 built-in ones.

You can work around this limitation by creating an array in the component class and referencing it within the template. You didn't specify your use case, so let's imagine that what you want is a counter which increments when the user clicks the div.

In your component, you create your counter array with its initial values:

counter = this.users.map(u => 0);

You can reference this within your template, using the index of the *ngFor to identify the correct element of the array.

<div *ngFor="let user of users; let i = index" (click)="counter[i] = counter[i] + 1">
   User clicked {{counter[i]}} times.
</div>

Working demo on stackblitz.

Tim
  • 5,435
  • 7
  • 42
  • 62
2

You can't create local variables. Sometimes you can get the index of the item in the array we are iterating over. You can do this by adding another variable to our ngFor expression and making it equal to index, like so:

<ul>
   <li *ngFor="let user of users; let i = index">
   {{ i + 1 }} - {{ user.name }}
   </li>
</ul>

If you need a variable in your component, you need to create it in component class before use it. For example:

@Component({
  selector: 'ngfor-example',
  template: `<h4>NgFor</h4>
 <ul>
   <li *ngFor="let user of users">
     {{ variable }} - {{ person.name }}
   </li>
 </ul>`
})
class NgForExampleComponent {
   variable = 0;
}
Junior Gantin
  • 2,102
  • 1
  • 18
  • 24
  • But in the case of a toggle of sorts, any of the items in the ngFor would set the variable for the entire component. What about if each user had a toggle button for example? – Ben Racicot Jan 21 '19 at 22:34