{"id":1994,"date":"2024-02-28T15:03:10","date_gmt":"2024-02-28T23:03:10","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1994"},"modified":"2024-02-28T15:03:10","modified_gmt":"2024-02-28T23:03:10","slug":"viewchild-and-contentchild-for-signal-based-queries","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/viewchild-and-contentchild-for-signal-based-queries\/","title":{"rendered":"viewChild() and contentChild() for signal-based queries"},"content":{"rendered":"\n<p>After introducing <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/model-for-signal-based-2-way-data-bindings\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>model()<\/code> for signal-based 2-way bindings<\/a>, let&#8217;s dive into <code><a href=\"https:\/\/angular.io\/api\/core\/viewChild\" target=\"_blank\" rel=\"noopener\" title=\"\">viewChild()<\/a><\/code> and <code><a href=\"https:\/\/angular.io\/api\/core\/contentChild\" target=\"_blank\" rel=\"noopener\" title=\"\">contentChild()<\/a><\/code> for signal-based queries.<\/p>\n\n\n\n<p>In case you&#8217;re not familiar with Angular queries, they existed before Angular 17.2 as the <code><a href=\"https:\/\/angular.dev\/api\/core\/ViewChild\" target=\"_blank\" rel=\"noopener\" title=\"\">@ViewChild<\/a><\/code> and <code><a href=\"https:\/\/angular.dev\/api\/core\/ContentChild\" target=\"_blank\" rel=\"noopener\" title=\"\">@ContentChild<\/a><\/code> decorators.<\/p>\n\n\n\n<p><strong>ViewChild<\/strong> is a way to get a reference on an HTML within the current component&#8217;s template. For instance, if we need our Typescript to access the instance of a given child component, I can use <code>@ViewChild<\/code> like so:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"188\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-26.png\" alt=\"\" class=\"wp-image-1995\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-26.png 617w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-26-300x91.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The new syntax gives us the same feature as a Signal:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"614\" height=\"211\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-27.png\" alt=\"\" class=\"wp-image-1996\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-27.png 614w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-27-300x103.png 300w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The same goes with <strong>ContentChild<\/strong>. The difference between <strong>ViewChild <\/strong>and <strong>ContentChild<\/strong> is that <strong>ViewChild <\/strong>looks for an element inside the component&#8217;s template, whereas <strong>ContentChild<\/strong> looks for an element projected by the parent component into the <code>ng-content<\/code> element of the current component using content projection. You can <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/passing-custom-content-to-a-component-with-content-projection\/\" target=\"_blank\" rel=\"noopener\" title=\"\">learn more about content projection with this tutorial<\/a>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"309\" height=\"293\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-28.png\" alt=\"\" class=\"wp-image-1997\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-28.png 309w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-28-300x284.png 300w\" sizes=\"auto, (max-width: 309px) 100vw, 309px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above code will look for an element with a <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/template-reference-variables\/\" target=\"_blank\" rel=\"noopener\" title=\"\">template reference variable<\/a> called <strong>test <\/strong>in the projected <code>ng-content<\/code>.<\/p>\n\n\n\n<p>We can achieve the same feature with the new <code>contentChild()<\/code> function, which returns a Signal:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"82\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-29.png\" alt=\"\" class=\"wp-image-1998\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-29.png 480w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-29-300x51.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Differences between these new functions and the old decorators<\/h3>\n\n\n\n<p>These new functions bring a few extra interesting features. For instance, we can get rid of possibly <code>undefined<\/code> values by making a query <code>required<\/code>:<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"65\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-30.png\" alt=\"\" class=\"wp-image-1999\" style=\"width:570px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-30.png 491w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-30-300x40.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Decorator-based queries were satisfied once the component was fully initialized, which means that running side effects required the use of specific lifecycle methods:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"193\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-31.png\" alt=\"\" class=\"wp-image-2000\" style=\"width:499px;height:auto\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-31.png 579w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-31-300x100.png 300w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>With signal-based queries, we receive a signal so we can rely on <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-computed\/\" target=\"_blank\" rel=\"noopener\" title=\"\">computed()<\/a><\/code> or <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-effect\/\" target=\"_blank\" rel=\"noopener\" title=\"\">effect()<\/a><\/code> to run such side-effects:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"110\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-32.png\" alt=\"\" class=\"wp-image-2001\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-32.png 580w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-32-300x57.png 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This approach is less intrusive and requires knowing just one thing: Signals! No more lifecycle methods are needed. You can find these different <a href=\"https:\/\/stackblitz.com\/edit\/at-viewchild-contentchild?file=src%2Fmain.ts,src%2Finfo.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">examples in action on Stackblitz here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After introducing model() for signal-based 2-way bindings, let&#8217;s dive into viewChild() and contentChild() for signal-based queries. In case you&#8217;re not familiar with Angular queries, they existed before Angular 17.2 as the @ViewChild and @ContentChild decorators. ViewChild is a way to get a reference on an HTML within the current component&#8217;s template. For instance, if we [&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,29],"tags":[],"class_list":["post-1994","post","type-post","status-publish","format-standard","hentry","category-angular","category-signals"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1994","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=1994"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":2003,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1994\/revisions\/2003"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}