Not entirely sure where you are confused, but let's break down what this line is doing.
<li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}</li>
First of all, the *ngFor command creates a number of list elements equal to the length of the "heroes" array. This is done using Angular's structural directives.
In clearer steps, the *ngFor statement looks at this line "let hero of heroes."
What this statement means is that for every variable in the array, heroes, a new < li > element is created. Then, within the scope of that created < li > element, it creates a variable "hero" which references the corresponding variable within the heroes array. This "hero" variable can be used and referenced only within that created < li > element.
For example, if you had an Array like this: myNumArray = [1,5,7,8],
and you had a list like this
<li *ngFor="let num of myNumArray" (click)="console.log(num)">{{num}}</li>
It would print out
1578Because the "num" variable in each < li > element refers back to the respective variable of myNumArray that was assigned when the element was created suing the *ngFor. Additionally, each < li > element would have a click event that logs its instance of num to the console. Clicking on 1, would log "1," clicking on 5 would log "5," and so on.
The reason you can refer to this local variable "hero" in the same element tag that the "ngFor" is called is because of some templating magic in Angular. the * symbol actually hops outside the element in which it was called and creates a template. You can read more about the utilization of * and ngFor here: https://angular.io/guide/structural-directives#the-asterisk--prefix
Hopefully this answers your question and explains how each particular "hero" info is created, and how they are distinct from each other.
(click)
is a click event. This means that when you click on the html element the event is attached to, it fires the specified function. That element has the data for each hero because the *ngFor is referencing the existingheroes
array which exists in the typescript. *ngFor is like a for loop, sohero
here refers to each individual hero, which contains the data. This data is passed toonSelect
when the element is clicked – jhhoff02