1
votes

In angular2 parent component, I have the following code, imgs is a private class variable. The below code can let image variable passed to its child component. That's exactly what I want.

However, I am confusing about the scope of image variable. It doesn't have any access modifier like public or private, but it acts like public variable. Can anyone explain how it works?

<li *ngFor="let image of imgs">
    <child [image]="image"></child>
</li>
2

2 Answers

1
votes

From the ngFor documentation page on the Angular 2 site

Class Description

The NgFor directive instantiates a template once per item from an iterable. The context for each instantiated template inherits from the outer context with the given loop variable set to the current item from the iterable.

0
votes

The image variable only exists within the template, and only within the closing tags of the element *ngFor was attached to. It only exists for the child component as the property it was bound to, not the same image variable.