{"id":686,"date":"2023-11-07T10:09:18","date_gmt":"2023-11-07T16:09:18","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=686"},"modified":"2023-11-07T10:09:18","modified_gmt":"2023-11-07T16:09:18","slug":"pass-data-from-component-to-component","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/pass-data-from-component-to-component\/","title":{"rendered":"Pass data from component to component"},"content":{"rendered":"\n<p>In Angular, you can pass data from one component to another using various methods, depending on the relationship between the components. Here are some common ways to pass data between components:<\/p>\n\n\n\n<ol>\n<li><strong>Using Input Properties<\/strong>: If you have a parent-child component relationship, you can pass data from a parent component to a child component using input properties. Here&#8217;s how to do it:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>In the parent component, define a property and bind it to an input property in the child component.<\/li>\n\n\n\n<li>In the child component, use the <code>@Input<\/code> decorator to receive the data. Parent Component:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ parent.component.ts\n   import { Component } from '@angular\/core';\n\n   @Component({\n     selector: 'app-parent',\n     template: `\n       &lt;app-child &#91;data]=\"message\"&gt;&lt;\/app-child&gt;\n     `,\n   })\n   export class ParentComponent {\n     message = 'Hello from Parent!';\n   }<\/code><\/pre>\n\n\n\n<p>Child Component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ child.component.ts\n   import { Component, Input } from '@angular\/core';\n\n   @Component({\n     selector: 'app-child',\n     template: `\n       &lt;p&gt;{{ data }}&lt;\/p&gt;\n     `,\n   })\n   export class ChildComponent {\n     @Input() data: string;\n   }<\/code><\/pre>\n\n\n\n<ol start=\"2\">\n<li><strong>Using Services<\/strong>: To share data between components that don&#8217;t have a direct parent-child relationship, you can use Angular services. Create a shared service and inject it into the components that need to exchange data.<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>In the service, define a property or method to store or provide the data.<\/li>\n\n\n\n<li>Inject the service into the components that need to access or modify the data. Service:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ data.service.ts\n   import { Injectable } from '@angular\/core';\n\n   @Injectable({\n     providedIn: 'root',\n   })\n   export class DataService {\n     sharedData: string = 'Hello from Service';\n   }<\/code><\/pre>\n\n\n\n<p>Components:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ component1.component.ts\n   import { Component } from '@angular\/core';\n   import { DataService } from '.\/data.service';\n\n   @Component({\n     selector: 'app-component1',\n     template: `\n       &lt;p&gt;{{ dataService.sharedData }}&lt;\/p&gt;\n     `,\n   })\n   export class Component1Component {\n     constructor(private dataService: DataService) {}\n   }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ component2.component.ts\n   import { Component } from '@angular\/core';\n   import { DataService } from '.\/data.service';\n\n   @Component({\n     selector: 'app-component2',\n     template: `\n       &lt;button (click)=\"updateData()\"&gt;Update Data&lt;\/button&gt;\n     `,\n   })\n   export class Component2Component {\n     constructor(private dataService: DataService) {}\n\n     updateData() {\n       this.dataService.sharedData = 'Data Updated';\n     }\n   }<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li><strong>Using Route Parameters<\/strong>: If you want to pass data between components based on route navigation, you can use route parameters. Configure your routes to accept parameters and access them in your components.<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Define route parameters in your route configuration.<\/li>\n\n\n\n<li>Access route parameters in your component using the <code>ActivatedRoute<\/code> service. Route Configuration:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   const routes: Routes = &#91;\n     { path: 'product\/:id', component: ProductComponent },\n   ];<\/code><\/pre>\n\n\n\n<p>Component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   import { Component } from '@angular\/core';\n   import { ActivatedRoute } from '@angular\/router';\n\n   @Component({\n     selector: 'app-product',\n     template: `\n       &lt;p&gt;Product ID: {{ productId }}&lt;\/p&gt;\n     `,\n   })\n   export class ProductComponent {\n     productId: string;\n\n     constructor(private route: ActivatedRoute) {\n       this.productId = this.route.snapshot.params&#91;'id'];\n     }\n   }<\/code><\/pre>\n\n\n\n<p>These are common methods for passing data between Angular components, and the approach you choose depends on your specific application and component relationships.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Angular, you can pass data from one component to another using various methods, depending on the relationship between the components. Here are some common ways to pass data between components: Child Component: Components: Component: These are common methods for passing data between Angular components, and the approach you choose depends on your specific application [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":260,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,101,49,40],"tags":[102,103,105,63],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/686"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=686"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":687,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/686\/revisions\/687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/260"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}