{"id":864,"date":"2023-04-26T05:00:00","date_gmt":"2023-04-26T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=864"},"modified":"2023-04-25T14:36:59","modified_gmt":"2023-04-25T21:36:59","slug":"how-to-create-your-own-two-way-data-binding-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-create-your-own-two-way-data-binding-with-angular\/","title":{"rendered":"How to create your own two-way data binding with Angular?"},"content":{"rendered":"\n<p>If you&#8217;ve ever used <code>[(ngModel)]<\/code> with Angular, you know what two-way data binding is. The idea is to have a variable that works simultaneously as an Input and an Output.<\/p>\n\n\n\n<p>In today&#8217;s post, we will see how to implement such a two-way data binding on our own so that we can use the <code>[()]<\/code> syntax on any of our component properties, such as <code>message<\/code> in the following code:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"136\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-45-1024x136.png\" alt=\"\" class=\"wp-image-866\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-45-1024x136.png 1024w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-45-300x40.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-45-768x102.png 768w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-45.png 1042w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Before we get into this tutorial, I want to tease that <a href=\"https:\/\/github.com\/angular\/angular\/discussions\/49682\" target=\"_blank\" rel=\"noopener\" title=\"\">signal-based components<\/a> will have such two-way data bindings available out of the box with the following syntax &#8211; using a <code>model()<\/code> function instead of the <code>@Input<\/code> and <code>@Output<\/code> decorators we&#8217;re used to:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"809\" height=\"474\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-46.png\" alt=\"\" class=\"wp-image-867\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-46.png 809w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-46-300x176.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-46-768x450.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note that the above feature will not be a part of the initial developer preview of <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-signals-are-in-the-works\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Signals<\/a> coming out in less than two weeks with Angular 16. However, it might be available in developer preview <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/angular-release-schedule\/\" target=\"_blank\" rel=\"noopener\" title=\"\">in six months<\/a> with Angular 17.<\/p>\n\n\n\n<p>In any case, here is the link to my <a href=\"https:\/\/blog.angulartraining.com\/tutorial-create-your-own-two-way-data-binding-in-angular-46487650ea82?source=friends_link&amp;sk=7c0eddae8756c7b2ab5f8e4c5f4c25bb\" target=\"_blank\" rel=\"noopener\" title=\"\">custom two-way data binding tutorial<\/a> and a <a href=\"https:\/\/stackblitz.com\/edit\/github-rsj8pz?file=projects%2Fdemos%2Fsrc%2Fapp%2Ftwo-way-data-binding-example%2Fmessage.component.ts,projects%2Fdemos%2Fsrc%2Fapp%2Ftwo-way-data-binding-example%2Ftwo-way-data-binding-example.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">code example in action on Stackblitz<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever used [(ngModel)] with Angular, you know what two-way data binding is. The idea is to have a variable that works simultaneously as an Input and an Output. In today&#8217;s post, we will see how to implement such a two-way data binding on our own so that we can use the [()] syntax [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8,7],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-angular","category-components","category-template-syntax"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=864"}],"version-history":[{"count":5,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/864\/revisions\/871"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}