Advertisement
Shell_Casing

class

Nov 19th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { ProductService } from '../../product.service';
  3. import { Router } from '@angular/router';
  4. import { ProductInterface } from '../../product-interface';
  5.  
  6. @Component({
  7.   selector: 'app-admin-products',
  8.   templateUrl: './admin-products.component.html',
  9.   styleUrls: ['./admin-products.component.css']
  10. })
  11. export class AdminProductsComponent implements OnInit {
  12.   products: ProductInterface[] = [];
  13.   filteredProducts: ProductInterface[];
  14.  
  15.   constructor(private productService: ProductService, private router: Router) { }
  16.  
  17.   ngOnInit() {
  18.     this.getProducts();
  19.   }
  20.  
  21.   editProduct(id) {
  22.     this.router.navigate([`/admin/products/${id}`]);
  23.   }
  24.  
  25.   deleteProduct(id) {
  26.     if (confirm('You are about to permanently remove this product. Continue?')) {
  27.       this.productService.deleteAProduct(id).subscribe(() => this.ngOnInit());
  28.     }
  29.   }
  30.  
  31.   getProducts() {
  32.     this.productService.getAllProducts().subscribe((products: ProductInterface[]) => {
  33.       console.log(products);
  34.       this.products = products;
  35.       this.filteredProducts = this.products;
  36.     });
  37.   }
  38.  
  39.   filterProducts(query) {
  40.     this.filteredProducts = query ?
  41.       this.products.filter(product => product.title.toLowerCase().includes(query.toLowerCase())) :
  42.       this.products;
  43.   }
  44.  
  45.   // the code above is equivalent to below:
  46.   // filterProducts(query) {
  47.   //   this.filteredProducts = this.products;
  48.   //   if (query) {
  49.   //     this.filteredProducts = this.products.filter(product => product.title.toLowerCase().includes(query.toLowerCase()));
  50.   //   } else {
  51.   //     this.filteredProducts = this.products;
  52.   //   }
  53.   // }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement