I have two components and a service in angular 4, the component one add a contact and the component two shows the contacts. I'm using HttpClient and Observables to get all the contacts, but when I add one contact my second component is not updating the contacts. I do it all this stuff through a service.The contacts displays well on start but they are not updating when I fire add() function.
My contact.service.ts
@Injectable()
export class ContactService {
  constructor(private http: HttpClient, private messageService: MessageService) { }
  private contactsUrl = 'api/contacts';
  getContacts(): Observable<Contact[]> {
      return this.http.get<Contact[]>(this.contactsUrl)
        .pipe(
            tap(contacts => this.log(`Fetched contacts`)),
            catchError(this.handleError('getContacts', []))
        );
  }
  getContact(id: number): Observable<Contact> {
      const url = `${this.contactsUrl}/${id}`;
      return this.http.get<Contact>(url).pipe(
      tap(_ => this.log(`fetched contact id=${id}`)),
      catchError(this.handleError<Contact>(`getContact id=${id}`))
    );
  }
  addContact (contact: Contact): Observable<Contact> {
      return this.http.post<Contact>(this.contactsUrl, contact, httpOptions).pipe(
          tap((contact: Contact) => this.log(`${contact.name} added to list`)),
          catchError(this.handleError<Contact>('addContact'))
      );
  }
}
My contact-item.component.ts
   export class ContactItemComponent implements OnInit {
  contacts: Contact[] = [];
  constructor(private contactService: ContactService) { }
  ngOnInit(){
      console.log("init");
      this.getContacts();
  }
  getContacts(): void {
      this.contactService.getContacts().subscribe(contacts => this.contacts = contacts);
  }
}
My contact-add.component.ts
export class ContactAddComponent {
  contacts: Contact[] = [];
  constructor(private contactService: ContactService) { }
 add(name: string, surname: string, phoneNumber: number): void {
      name = name.trim();
      surname = surname.trim();
      phoneNumber = phoneNumber;
      if(!name || !surname || !phoneNumber) { return; }
      this.contactService.addContact({ name, surname, phoneNumber } as Contact)
        .subscribe(contact => {
            this.contacts.push(contact);
        })
  }
 
     
     
    