1

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.