Guest User

Untitled

a guest
Oct 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Contact } from './Contact';
  3.  
  4. @Injectable({
  5. providedIn: 'root'
  6. })
  7. export class ContactsService {
  8.  
  9. contacts: Contact[] =
  10. [
  11. {
  12. id: 1,
  13. first: "Chad",
  14. last: "Michel",
  15. twitter: "@chadmichel"
  16. },
  17. {
  18. id: 2,
  19. first: "Bill",
  20. last: "Udell",
  21. twitter: "@billudell"
  22. },
  23. {
  24. id: 3,
  25. first: "Russ",
  26. last: "Guill",
  27. twitter: "@russwritesne"
  28. },
  29. ];
  30.  
  31. constructor() { }
  32.  
  33. all(): Promise<Contact[]> {
  34.  
  35. var promise = new Promise<Contact[]>((resolve, reject) => {
  36. setTimeout(() => {
  37. resolve(this.contacts);
  38. });
  39. });
  40. return promise;
  41.  
  42. }
  43.  
  44. find(id: number): Promise<Contact> {
  45.  
  46. var promise = new Promise<Contact>((resolve, reject) => {
  47. setTimeout(() => {
  48. this.contacts.forEach(element => {
  49. if (element.id == id)
  50. resolve(element);
  51. });
  52. });
  53. });
  54. return promise;
  55.  
  56. }
  57.  
  58. save(contact: Contact) {
  59. this.contacts.forEach(c => {
  60. if (c.id == contact.id) {
  61. c.first = contact.first;
  62. c.last = contact.last;
  63. c.twitter = contact.twitter;
  64. }
  65. });
  66. }
  67.  
  68. }
Add Comment
Please, Sign In to add comment