Fireship · @fireship

Sharing Data between Components in Angular

Uploaded April 30, 2017 · Length 5:19

Sharing Data between Components in Angular

Summary

angular data sharing methods including input decorator, event emitter, view child, and shared services.

  • angular data sharing
  • angular input decorator
  • angular event emitter
  • angular view child
  • angular shared services

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:01

    in this episode I'm going to show you

  2. 0:02

    four different ways to share data

  3. 0:05

    between angular

  4. 0:06

    components parent to child via the input

  5. 0:10

    [Music]

  6. 0:14

    decorator when you declare a variable

  7. 0:16

    with the input decorator in the child

  8. 0:18

    component it allows that variable to be

  9. 0:21

    received from the parent component

  10. 0:23

    template in this case we Define a

  11. 0:25

    message variable in the parent then use

  12. 0:28

    square brackets to pass the data to the

  13. 0:31

    child now the child component can

  14. 0:33

    display the data in its own

  15. 0:35

    [Music]

  16. 0:41

    template child to parent via output and

  17. 0:45

    event emitter another way to share data

  18. 0:48

    is to emit it from the child which can

  19. 0:50

    be listened to by the parent this

  20. 0:53

    approach is ideal when you want to share

  21. 0:55

    data changes that occur on things like

  22. 0:57

    button clicks form entries and other

  23. 0:59

    user

  24. 1:02

    events in the child we declare a message

  25. 1:05

    event variable with the output decorator

  26. 1:08

    and set it equal to a new event

  27. 1:11

    [Music]

  28. 1:15

    emitter then we create a function named

  29. 1:17

    send message that calls emit on this

  30. 1:20

    event with the message we want to

  31. 1:27

    send lastly we create a button to

  32. 1:29

    trigger the this function in the

  33. 1:32

    [Music]

  34. 1:45

    HTML in the parent we create a function

  35. 1:47

    to receive the message and set it equal

  36. 1:50

    to the message

  37. 1:52

    variable the parent can now subscribe to

  38. 1:54

    this message event that's outputed by

  39. 1:56

    the child component then run the receive

  40. 1:58

    message function whenever that event