{"id":684,"date":"2023-11-07T10:07:25","date_gmt":"2023-11-07T16:07:25","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=684"},"modified":"2023-11-07T10:07:25","modified_gmt":"2023-11-07T16:07:25","slug":"output-quick-example-in-angular","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/output-quick-example-in-angular\/","title":{"rendered":"@Output() quick example in angular"},"content":{"rendered":"\n<p>In Angular is possible to pass data from a child component to a parent component using the <code>@Output<\/code> decorator and an EventEmitter, like this:<\/p>\n\n\n\n<ol>\n<li><strong>Child Component (Sender)<\/strong>: In the child component, define an <code>@<\/code><strong><code>Output<\/code> <\/strong>property with an <code>EventEmitter<\/code>. <\/li>\n\n\n\n<li>This property will emit an event containing the data you want to pass to the parent component.<\/li>\n<\/ol>\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=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">   import { Component, EventEmitter, Output } from '@angular\/core';\n\n   @Component({\n     selector: 'app-child',\n     template: `\n       &lt;button (click)=\"sendData()\">Send Data to Parent&lt;\/button>\n     `,\n   })\n   export class ChildComponent {\n     @Output() dataEvent = new EventEmitter&lt;string>();\n\n     sendData() {\n       const dataToSend = 'Data from Child';\n       this.dataEvent.emit(dataToSend);\n     }\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"2\">\n<li><strong>Parent Component (Receiver)<\/strong>: \n<ul>\n<li>In the parent component, need to bind to the <code>@Output<\/code> property of the child component and define a method to handle the emitted event.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#FB8CFF\"><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=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">   import { Component } from '@angular\/core';\n   @Component({\n     selector: 'app-parent',\n     template: `\n       &lt;app-child (dataEvent)=\"receiveData($event)\">&lt;\/app-child>\n       &lt;p>Data received from Child: {{ receivedData }}&lt;\/p>\n     `,\n   })\n   export class ParentComponent {\n     receivedData: string;\n\n     receiveData(data: string) {\n       this.receivedData = data;\n     }\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<p>In the parent component&#8217;s template, <code>(dataEvent)=\"receiveData($event)\"<\/code> binds to the <code>dataEvent<\/code> event of the child component and calls the <code>receiveData<\/code> method when the event is emitted.<\/p>\n\n\n\n<p>With this setup, when the &#8220;Send Data to Parent&#8221; button in the child component is clicked, it emits the <code>dataEvent<\/code> event with the data &#8220;Data from Child.&#8221; <\/p>\n\n\n\n<p>The parent component captures this event and updates the <strong><code>receivedData<\/code> <\/strong>property, which is displayed in the parent component&#8217;s template.<\/p>\n\n\n\n<p>And it&#8217;s done!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Angular is possible to pass data from a child component to a parent component using the @Output decorator and an EventEmitter, like this: In the parent component&#8217;s template, (dataEvent)=&#8221;receiveData($event)&#8221; binds to the dataEvent event of the child component and calls the receiveData method when the event is emitted. With this setup, when the &#8220;Send [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":261,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,52,49],"tags":[102,103,105,63],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/684"}],"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=684"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/684\/revisions"}],"predecessor-version":[{"id":685,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/684\/revisions\/685"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/261"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}