Fireship · @fireship
Sharing Data between Components in Angular
Uploaded April 30, 2017 · Length 5:19
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.
- 0:01
in this episode I'm going to show you
- 0:02
four different ways to share data
- 0:05
between angular
- 0:06
components parent to child via the input
- 0:10
[Music]
- 0:14
decorator when you declare a variable
- 0:16
with the input decorator in the child
- 0:18
component it allows that variable to be
- 0:21
received from the parent component
- 0:23
template in this case we Define a
- 0:25
message variable in the parent then use
- 0:28
square brackets to pass the data to the
- 0:31
child now the child component can
- 0:33
display the data in its own
- 0:35
[Music]
- 0:41
template child to parent via output and
- 0:45
event emitter another way to share data
- 0:48
is to emit it from the child which can
- 0:50
be listened to by the parent this
- 0:53
approach is ideal when you want to share
- 0:55
data changes that occur on things like
- 0:57
button clicks form entries and other
- 0:59
user
- 1:02
events in the child we declare a message
- 1:05
event variable with the output decorator
- 1:08
and set it equal to a new event
- 1:11
[Music]
- 1:15
emitter then we create a function named
- 1:17
send message that calls emit on this
- 1:20
event with the message we want to
- 1:27
send lastly we create a button to
- 1:29
trigger the this function in the
- 1:32
[Music]
- 1:45
HTML in the parent we create a function
- 1:47
to receive the message and set it equal
- 1:50
to the message
- 1:52
variable the parent can now subscribe to
- 1:54
this message event that's outputed by
- 1:56
the child component then run the receive
- 1:58
message function whenever that event


