Guest User

Untitled

a guest
Jun 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. export class ProductService {
  2. public products: Product[];
  3.  
  4. constructor(private http: HttpClient) { }
  5.  
  6. init(): any {
  7. if (!this.products) {
  8. this.http.get<Product[]>("https://my-api.com/GetAllProducts").subscribe(products => {
  9. this.products = products;
  10. });
  11. }
  12. }
  13. }
  14.  
  15. getProductFromProductId(productId: string) {
  16. return this.products.find(x => x.id === productId);
  17. }
  18.  
  19. getProductFromProductId(productId: string) {
  20. if(!this.products) {
  21. setTimeout(() => {
  22. console.log("Products not loaded yet");
  23. getProductFromProductId(productId);
  24. }, 10);
  25. } else {
  26. return this.products.find(x => x.id === productId);
  27. }
  28. }
Add Comment
Please, Sign In to add comment