Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. /* Description:
  2. * A Product Item "Add to cart" Button Component was clicked... So the Event Emmiter is called passing the
  3. * event emmiter name (in this case "addToCart" is the event name).
  4. * The "EventEmmiterService" is responsible to hearing when the Event occurs and manage the emmiters triggered...
  5. * When the event is triggered, the cart component receive the emmited event, because it was subscribed
  6. * to the "addToCart" event (triggered on product-item.component.ts)
  7. */
  8.  
  9. //////////////////////////////////////////////////////////// product-item.component.html
  10.  
  11. ...
  12. <button (click)=addToCart(product)>
  13. Add to cart
  14. </button>
  15. ...
  16.  
  17. ///////////////////////////////////////////////////////////// product-item.component.ts
  18.  
  19. import { Component, Input, Output } from '@angular/core';
  20. import { EventEmitterService } from '../service/event-emitter.service';
  21.  
  22. @Component({
  23. selector: 'product-item',
  24. templateUrl: './product-item.component.html',
  25. styleUrls: ['./product-item.component.scss']
  26. })
  27. export class ProductItemComponent {
  28.  
  29. @Input()
  30. public product: any
  31.  
  32. addToCart(item) {
  33. EventEmitterService.get('addToCart').emit(item);
  34. }
  35. }
  36.  
  37. /////////////////////////////////////////////////////////////////// cart.component.ts
  38.  
  39. import { Component, OnInit } from '@angular/core';
  40. import { EventEmitterService } from '../service/event-emitter.service';
  41.  
  42. @Component({
  43. selector: 'app-cart-details',
  44. templateUrl: './cart.component.html',
  45. styleUrls: ['./cart.component.scss']
  46. })
  47. export class CartComponent implements OnInit {
  48.  
  49. ngOnInit() {
  50. EventEmitterService.get('addToCart').subscribe(item=> console.log(item, `Item Added using Event Emmitter!!!`))
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement