I am very new in Angular 2 and I have the following question about services.
Into the main view (the one related to the app.component.ts class) I have this situation:
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-8 col-md-offset-2">
<app-new-account (accountAdded)="onAccountAdded($event)"></app-new-account>
<hr>
<app-account
*ngFor="let acc of accounts; let i = index"
[account]="acc"
[id]="i"
(statusChanged)="onStatusChanged($event)"></app-account>
</div>
</div>
</div>
So into this view I have 2 sub component (app-new-account and app-account).
Into the main AppComponent component class I have:
import {Component, OnInit} from '@angular/core';
import {AccountsService} from './accounts.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [AccountsService]
})
export class AppComponent implements OnInit {
accounts: {name: string, status: string}[] = [];
// Injectiong the AccountsService:
constructor(private accountsService: AccountsService) {}
ngOnInit() {
this.accounts = this.accountsService.accounts;
}
}
Where I am defining the AccountsService as a service by this line into the component decorator:
providers: [AccountsService]
From what I have understood it specify that this class is AccountsService have to be registered as service of the AppComponent main component and for all its subcomponent. Is it this assertion true or am I missing something?
So, it means that the two sub components classes related to the previous app-new-account and app-account tags share the same instance of the AccountsService class as service?
Is this the reason because in the providers array of these 2 sub somponents I have not the AccountsService?
Is it my reasoninc correct or am I missing something?