I have questions about transferring data to Angular.
Firstly, I don't have a structure like <parent><child [data]=parent.data></child></parent>
My structure
<container> <navbar> <summary></summary> <child-summary><child-summary> </navbar> <content></content> </container>
So, in <summary/> I have a selection that sends the value to <child-summary/> and <content/> .
The OnSelect method starts well with (change) inside the <summary/> component.
So, I tried with @Input , @Output and @EventEmitter , but I donβt see how to extract the event as an @Input component, unless I switch to the parent / child template. All the examples that I founded are related between the components.
EDIT: The example with BehaviorSubject does not work (all connected services to the API work well, only the observable starts at startup, but not when the choice matters has changed)
shared service = company.service.ts (used to get company data)
import { Injectable } from '@angular/core'; import { Headers, Http, Response } from '@angular/http'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/toPromise'; @Injectable() export class SrvCompany { private accountsNumber = new BehaviorSubject<string[]>([]); currentAccountsNumber = this.accountsNumber.asObservable(); changeMessage(accountsNumber: string[]) { this.accountsNumber.next(accountsNumber); } private _companyUrl = 'api/tiers/'; constructor(private http: Http) { } getSociete(): Promise<Response> { let url = this._companyUrl; return this.http.get(url).toPromise(); } }
invoice.component.ts ("child")
import { Component, OnInit, Input } from '@angular/core'; import { Headers, Http, Response } from '@angular/http'; import { SrvInvoice } from './invoice.service'; import { SrvCompany } from '../company/company.service'; @Component({ selector: 'invoice', templateUrl: 'tsScripts/invoice/invoice.html', providers: [SrvInvoice, SrvCompany] }) export class InvoiceComponent implements OnInit { invoice: any; constructor(private srvInvoice: SrvInvoice, private srvCompany: SrvCompany) { } ngOnInit(): void {
company.component.ts (parent trigger)
import { Component, Inject, OnInit, Input } from '@angular/core'; import { Headers, Http, Response } from '@angular/http'; import { SrvCompany } from './company.service'; @Component({ selector: 'company', templateUrl: 'tsScripts/company/company.html', providers: [SrvCompany] }) export class CompanyComponent implements OnInit { societes: any[]; soc: Response[];