{"flag":true,"single":true,"pageTitle":"What is Child Component | Custom Property Binding | @Input()| Angular 15+","post":{"id":59,"user_id":"1","slug":"what-is-child-component-custom-property-binding-components-angular-15--zlvj","title":"What is Child Component | Custom Property Binding | @Input()| Angular 15+","body":"<p>We can pass data from component class to view template and vice versa&nbsp;using property binding, string interpolation and event binding.<br>We can also pass data from parent component to child component and&nbsp;vice versa. We use <strong>@lnput &amp; @Output<\/strong> decorator for that.<\/p>\r\n<p>You can use your components in other components as well instedd of parent component<\/p>\r\n<p><strong>Custom Property Binding&nbsp;<\/strong><\/p>\r\n<p>We can pass data from parent component to child component using&nbsp;@lnput decorator.<\/p>\r\n<p>We also call it custom property binding because here we bind the custom&nbsp;properties of child component class with the property or method of&nbsp;parent component class.<\/p>\r\n<p>1. First of all import Input module in child component<\/p>\r\n<pre class=\"language-markup\"><code>import { Component, Input } from '@angular\/core';<\/code><\/pre>\r\n<p>2. Add Input decorator in child component, it will be used to get data from parent component<\/p>\r\n<pre class=\"language-markup\"><code>@Input() fromParentIGotvalue:number = 0;<\/code><\/pre>\r\n<p>SO HERE is example:we create parent component<\/p>\r\n<p><strong>products.component.html<\/strong><\/p>\r\n<p><strong>[CHILDCOMPONENT-VARIABLE] =\"PARENTCOMPONENT\"<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;div&gt;\r\n\t&lt;app-filter [fromParentIGotvalue]=\"ihaveProducts\"&gt;&lt;\/app-filter&gt;\r\n\tI have {{ihaveProducts}} Products\r\n&lt;\/div&gt;<\/code><\/pre>\r\n<p><strong>products.component.js<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>export class ProductsComponent {\r\n    ihaveProducts:number = 50;\r\n    IhaveFreeProducts = 12;\r\n}<\/code><\/pre>\r\n<p>app-filter is child component in parent, and we pass <strong>IhaveFreeProducts <\/strong>variable to that child component<\/p>\r\n<p>Now access this in child component<\/p>\r\n<p><strong>filter.component.html<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;p&gt;filter works! and have {{freeCourses}} free courses&lt;\/p&gt;\r\n&lt;p&gt;In parent I found  {{freeproducts}} free courses&lt;\/p&gt;<\/code><\/pre>\r\n<p><strong>filter.component.ts<\/strong><\/p>\r\n<p>we import Input and use decorater to access the value<\/p>\r\n<pre class=\"language-markup\"><code>import { Component, Input } from '@angular\/core';\r\n\r\n@Component({\r\n  selector: 'app-filter',\r\n  templateUrl: '.\/filter.component.html',\r\n  styleUrls: ['.\/filter.component.css']\r\n})\r\nexport class FilterComponent {\r\n  freeCourses:number = 10;\r\n  @Input() freeproducts:number = 0;\r\n}\r\n<\/code><\/pre>","category_id":"13","is_private":"0","created_at":"2023-03-27T23:09:25.000000Z","updated_at":"2023-03-31T03:02:54.000000Z","category":{"id":13,"user_id":"1","name":"Angular","slug":"angular-rfjp","parent_id":"12","created_at":"2023-03-24T03:25:15.000000Z","updated_at":"2023-03-24T03:25:15.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"We can pass data from component class to view template and vice versa&nbsp;using property binding, string interpolation and event binding.We - What is Child Component | Custom Property Binding | @Input()| Angular 15+ (Updated: March 31, 2023) - Read more about What is Child Component | Custom Property Binding | @Input()| Angular 15+ at my programming site [SITE]","categories":[]}