{"id":994,"date":"2023-05-25T05:00:00","date_gmt":"2023-05-25T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=994"},"modified":"2023-05-18T15:18:32","modified_gmt":"2023-05-18T22:18:32","slug":"signals-computed","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/signals-computed\/","title":{"rendered":"Signals: computed()"},"content":{"rendered":"\n<p>After introducing <a href=\"https:\/\/blog.angulartraining.com\/angular-signals-best-practices-around-exposing-signals-5385452150a1\" target=\"_blank\" rel=\"noopener\" title=\"\">how to create Signals<\/a> and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/three-ways-to-update-angular-signals\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to update them<\/a>, let&#8217;s take a look at one more exciting feature that helps replace the need for RxJs Observables.<\/p>\n\n\n\n<p><strong>How to emit a new Signal value when one or more Signals get updated?<\/strong> That&#8217;s what <code><a href=\"https:\/\/angular.io\/api\/core\/computed\" target=\"_blank\" rel=\"noopener\" title=\"\">computed()<\/a><\/code> does. In my <a href=\"https:\/\/angular-training.getlearnworlds.com\/course\/angular-signals\" target=\"_blank\" rel=\"noopener\" title=\"\">Signals course<\/a>, I illustrate <code>computed()<\/code> with the following example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"37\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-30.png\" alt=\"\" class=\"wp-image-995\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-30.png 703w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-30-300x16.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In the above code, <code>this.rates()<\/code> and <code>this.currency()<\/code> are two different Signals. <code>this.rates()<\/code> emits up-to-date exchange rates for all currencies in the world. <code>this.currency()<\/code> emits the current currency selected by the user.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><code>computed()<\/code> takes a function as a parameter. The function returns the computed value from my two Signals; in this case, the up-to-date exchange rate for the current currency. If the exchange rates or the currency get updated, this computed Signal will emit an updated value automatically.<\/p>\n\n\n\n<p>This is somewhat similar to combining several Observables and using <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-switchmap-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\">switchMap<\/a><\/code> or <code><a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-combinelatest-operator\/\" target=\"_blank\" rel=\"noopener\" title=\"\">combineLatest<\/a><\/code> to get a customized result. It&#8217;s a lot easier with Signals (one line of code!).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After introducing how to create Signals and how to update them, let&#8217;s take a look at one more exciting feature that helps replace the need for RxJs Observables. How to emit a new Signal value when one or more Signals get updated? That&#8217;s what computed() does. In my Signals course, I illustrate computed() with the [&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-994","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\/994","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=994"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/994\/revisions"}],"predecessor-version":[{"id":997,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/994\/revisions\/997"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}