I am pretty new in Angular 2 and I am not so into JavaScript\TypeScript (I came from Java) and I have some doubt about an example that I am studying related how to a component can use a property defined in another component (property\event binding). The example show how an array of element declared in a child component can be used and shown in the parent component.
So I have the app-component that is the parent component.
This is the app-component "view" named app-component.html:
<div class="container">
<app-cockpit></app-cockpit>
<hr>
<div class="row">
<div class="col-xs-12">
<app-server-element
*ngFor="let serverElement of serverElements"
[element]="serverElement"></app-server-element>
</div>
</div>
</div>
From what I can undestand this code generate app-server-element (another component of my application with its layout) starting from the serverElements array that is defined into the app.component.ts class, this one:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
serverElements = [{type: 'server', name: 'TestServer', content: 'Just a Test'}];
}
So here I have a this first doubt: doing:
<app-server-element
*ngFor="let serverElement of serverElements"
[element]="serverElement">
</app-server-element>
The *ngFor directive is iterating on the serverElements array that is defined into the app.component.ts class (related to the main app-component component) and not on the server-element.component.ts class (related to the component).
It works fine but it seems to me a little strange. Why? My idea is that this *ngFor directive is declared into the HTML code of the app-component.html related to the , so the iteration is on an array that is defined in the related class.
Is it or am I missing something?