service-contactrxjs.service.ts 793 Bytes
Newer Older
Ooh-Ao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import { Injectable } from '@angular/core';
import { from, Observable } from 'rxjs';
import { Contact } from './contact';
import { contacts } from './contact-data';



@Injectable({
  providedIn: 'root'
})
export class ServiceContactrxjsService {

  constructor() { }

  private contacts: Contact[] = contacts;



  // getContacts(): Observable<Contact> {
  // You can also fetch data from Api using HttpClient.
  //   return this.http.get(url..)
  // }
  getContacts(): Observable<Contact> {
    return from(this.contacts)

  }

  deleteContact(contact: Contact): void {
    this.contacts = this.contacts.filter(Contact =>
      Contact.firstName !== contact.firstName && Contact.lastName !== contact.lastName);
  }

  addContact(con: Contact): void {
    this.contacts?.push(con);
  }


}