{"id":1972,"date":"2021-05-05T12:00:00","date_gmt":"2021-05-05T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/passing-variables-between-components-in-angular-parent-child-path-param-service-and-router\/"},"modified":"2026-07-05T03:25:27","modified_gmt":"2026-07-05T01:25:27","slug":"passing-variables-between-components-in-angular-parent-child-path-param-service-and-router","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/passing-variables-between-components-in-angular-parent-child-path-param-service-and-router\/","title":{"rendered":"Passing Variables Between Components in Angular (Parent, Child, Path Param, Service and Router)"},"content":{"rendered":"<p>I would show you several way you can pass variables from one component to another in Angular. We would cover the following;<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><a href=\"#t1\">Set up and Angular App<\/a><\/li>\n<li><a href=\"#t2\">Pass Variables from Parent to Child<\/a><\/li>\n<li><a href=\"#t3\">Pass Variable from Child to Parent<\/a><\/li>\n<li><a href=\"#t4\">Using State Variable<\/a><\/li>\n<li><a href=\"#t5\">Using Path Parameters<\/a><\/li>\n<li><a href=\"#t6\">Pass Variables via a Service<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Setup the Environment<\/strong><\/h4>\n<p>First create a new Angular app. I call it <em>variabledemo<\/em>. Remember to add routing<\/p>\n<p>Create the parent component. Add the parent route<\/p>\n<p>Create the child component. Add the child route<\/p>\n<p>Remove all the content of the index<\/p>\n<p>Embed the child component into the parent component using the &lt;app-child&gt;&lt;\/app-child&gt; tag.<\/p>\n<p>Test your app!<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Pass Variables from Parent to Child<\/strong><\/h4>\n<p>In the parent component ts file, declare a string variable. I call it parentName. On the ngOnInit() method, I assign it a value &#8220;Kindson the Genius&#8221;.<\/p>\n<p>So we want the child to use this variable. Somehow, we need to pass this variable to the child component. Then the child component would render the value of this variable to the page.<\/p>\n<p>The child component would receive this variable and in a local variable annotated with @Input annotation.<\/p>\n<p>So declare a local variable in the child component called receivedValue. Annotate it with the @Input annotation.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Input<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #997700; font-weight: bold;\">receivedValue:<\/span> String<span style=\"color: #333333;\">;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now to pass the parent&#8217;s parentName variable to the child&#8217;s receivedValue variable we add it as an attribute to the app-child markup using the format:<\/p>\n<p>[childVariable] = &#8220;parentVariable&#8221;<\/p>\n<p>At this point, the value of the parentVariable would then be available to the childVariable as well. So my markup looks like this:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>app<span style=\"color: #333333;\">-<\/span>child <span style=\"color: #333333;\">[<\/span>receivedValue<span style=\"color: #333333;\">]=<\/span><span style=\"background-color: #fff0f0;\">\"parentName\"<\/span><span style=\"color: #333333;\">&gt;&lt;\/<\/span>app<span style=\"color: #333333;\">-<\/span>child<span style=\"color: #333333;\">&gt;<\/span>\r\n<\/pre>\n<h4><\/h4>\n<h4><strong id=\"t3\">3. Pass Variable from Child to Parent<\/strong><\/h4>\n<p>Let&#8217;s first create the variable to send. I call it valueToSend and initialize\u00a0 it to &#8220;The Tech Pro from Child&#8221;.<\/p>\n<p>To pass a variable from child to parent, we need something called an emitter. This is what actually does the sending. It is simply a variable(function) of type EventEmitter and would be annotated with @Output.<\/p>\n<p>This variable would be of type event emitter declared like this:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Output<\/span><span style=\"color: #333333;\">()<\/span> sender <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> EventEmitter<span style=\"color: #333333;\">();<\/span>\r\n<\/pre>\n<p>To actually, send this variable we need to call the emit() method of the EventEmitter variable like so:<\/p>\n<p>Then, in the parent component, we need a function (sort of a receiver) to actually capture what is sent by the child.<\/p>\n<p>So in the parent component create a function receiver like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">receiver<span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">receivedFromChild:<\/span>any<span style=\"color: #333333;\">){<\/span>\r\n  console<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">log<\/span><span style=\"color: #333333;\">(<\/span>receivedFromChild<span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Then in the html markup, you can then connect the sender(from child) and the receiver(in parent) using this format:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>app<span style=\"color: #333333;\">-<\/span>child <span style=\"color: #333333;\">(<\/span>sender<span style=\"color: #333333;\">)=<\/span><span style=\"background-color: #fff0f0;\">\"receiver($event)\"<\/span><span style=\"color: #333333;\">&gt;&lt;\/<\/span>app<span style=\"color: #333333;\">-<\/span>child<span style=\"color: #333333;\">&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s now see how to pass variables between sibling components on the same level.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Pass Variables Using State Variable<\/strong><\/h4>\n<p>Let&#8217;s first create two more components: child1 and child2<\/p>\n<p>Then we add routing for the two components as well<\/p>\n<p>Then add navigation link to them as well from the parent component.<\/p>\n<p>Now, this is all fine. But we&#8217;ll now adjust to be able to navigate using a router.<\/p>\n<p>So add router services the the child components<\/p>\n<p>Also add a button that has a click event and set up the click function. The this function would be like this:<\/p>\n<p><strong>Button Markup<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;button<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">\"button\"<\/span> <span style=\"color: #ff0000;\">(<\/span><span style=\"color: #0000cc;\">click<\/span><span style=\"color: #ff0000;\">)=\"<\/span><span style=\"color: #0000cc;\">goto2<\/span><span style=\"color: #ff0000;\">()\"<\/span><span style=\"color: #007700;\">&gt;<\/span> Go to Child 2<span style=\"color: #007700;\">&lt;\/button&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Event Handler<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  goto2(){\r\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span>.router.navigate([<span style=\"background-color: #fff0f0;\">\"\/child2\"<\/span>])\r\n  }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now we would pass the state variable as a second parameter to the navigate function, like so;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  goto2(){\r\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span>.router.navigate([<span style=\"background-color: #fff0f0;\">\"\/child2\"<\/span>], {state<span style=\"color: #333333;\">:<\/span> {data<span style=\"color: #333333;\">:<\/span><span style=\"color: #008800; font-weight: bold;\">this<\/span>.Child1Msg}})\r\n  }\r\n<\/pre>\n<p>This means that we pass a state variable along with the route to child2<\/p>\n<p>Now we would receive the variable in child2 using <strong><em>history.state.<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Pass Variables Using Path Parameter<\/strong><\/h4>\n<p>In this method, the variable would show up in the url of the request. So let&#8217;s pass path parameter from Child to Child1. Let&#8217;s call the parameter msg.<\/p>\n<p>First, open the app-routing component and adjust the route to child1 to allow for path parameter. Adjust like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">{<\/span> <span style=\"color: #997700; font-weight: bold;\">path:<\/span> <span style=\"background-color: #fff0f0;\">\"child1\/:msg\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #997700; font-weight: bold;\">component:<\/span> Child1Component<span style=\"color: #333333;\">},<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, in the routing code in Child component, adjust the goto function to add the msg value like so:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">  goto1<span style=\"color: #333333;\">(){<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">router<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">navigate<\/span><span style=\"color: #333333;\">([<\/span><span style=\"background-color: #fff0f0;\">\"\/child1\/\"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">valueToSend<\/span><span style=\"color: #333333;\">])<\/span>\r\n  <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally in Child1 component, we retrieve this variable by two steps:<\/p>\n<p>1. inject an active route to the component like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">private<\/span> <span style=\"color: #997700; font-weight: bold;\">route:<\/span> ActivatedRoute<\/pre>\n<p>2. use route.snapshot.params to get the parameter like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">route<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">snapshot<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">params<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">msg<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Pass Variables via a Service<\/strong><\/h4>\n<p>Finally, you may have to pass data using services. Although you may not need this often, but the benefit is that you now have your data preserved in a separate service. So you can already read the value from the service.<\/p>\n<p>In this method, you simply create a service and wire it into the components.<\/p>\n<p>First create a service using ng generate service. I call it proxy.<\/p>\n<p>Here is the content of this service:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> ProxyService {\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">public<\/span> variable1<span style=\"color: #333333;\">:<\/span> <span style=\"color: #007020;\">String<\/span>; <span style=\"color: #888888;\">\/\/This is the variable<\/span>\r\n\r\n  constructor() { }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So, I&#8217;m going to be stopping here, bu I do recommend you watch the video on my <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\">YouTube Channel<\/a> where this is done step by step with some fun.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would show you several way you can pass variables from one component to another in Angular. We would cover the following; &nbsp; Set up &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1972","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1972"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1972\/revisions"}],"predecessor-version":[{"id":2140,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1972\/revisions\/2140"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}