{"id":673,"date":"2023-10-16T10:41:43","date_gmt":"2023-10-16T16:41:43","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=673"},"modified":"2023-10-16T10:41:43","modified_gmt":"2023-10-16T16:41:43","slug":"input-example-in-angular","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/input-example-in-angular\/","title":{"rendered":"@Input() example in Angular"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Certainly! Here&#8217;s an example of how to use the <code>@Input<\/code> decorator in Angular to pass data from a parent component to a child component:<\/p>\n\n\n\n<ol>\n<li>Create a child component: Let&#8217;s create a simple child component that receives data from its parent component.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   ng generate component child<\/code><\/pre>\n\n\n\n<p>In <code>child.component.ts<\/code>, define an <code>@Input<\/code> property to receive data from the parent component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">   import { Component, Input } from '@angular\/core';\n\n   @Component({\n     selector: 'app-child',\n     template: `\n       &lt;div>\n         &lt;h2>Child Component&lt;\/h2>\n         &lt;p>Data from parent: {{ dataFromParent }}&lt;\/p>\n       &lt;\/div>\n     `,\n   })\n   export class ChildComponent {\n     @Input() dataFromParent: string;\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"2\">\n<li>Create a parent component: In your parent component, you can pass data to the child component using the <code>@Input<\/code> property. In <code>parent.component.html<\/code>, include the child component and bind the <code>dataFromParent<\/code> property:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#abb8c3\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-php\">   &lt;div>\n     &lt;h1>Parent Component&lt;\/h1>\n     &lt;app-child [dataFromParent]=\"message\">&lt;\/app-child>\n   &lt;\/div><\/code><\/pre><\/div><\/div>\n\n\n\n<p>In <code>parent.component.ts<\/code>, define the <code>message<\/code> property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<\/code><\/pre>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#abb8c3\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" no-line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-php\">import { Component } from '@angular\/core';\n\n@Component({\n     selector: 'app-parent',\n     templateUrl: '.\/parent.component.html',\n   })\n   export class ParentComponent {\n     message = 'Hello from the parent component!';\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"3\">\n<li>Use the parent component: In your app module, make sure to include both the parent and child components in the declarations section of the module.<\/li>\n\n\n\n<li>Run your application: Run your Angular application to see the <code>@Input<\/code> in action:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   ng serve<\/code><\/pre>\n\n\n\n<p>The child component receives and displays the <code>dataFromParent<\/code> value passed from the parent component.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Certainly! Here&#8217;s an example of how to use the @Input decorator in Angular to pass data from a parent component to a child component: In child.component.ts, define an @Input property to receive data from the parent component: In parent.component.ts, define the message property: The child component receives and displays the dataFromParent value passed from the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/673"}],"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=673"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/673\/revisions"}],"predecessor-version":[{"id":674,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/673\/revisions\/674"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}