<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Angular Shots - Medium]]></title>
        <description><![CDATA[Community-driven Angular how-to collection. For contribution or collaboration tweet or DM! - Medium]]></description>
        <link>https://medium.com/angular-shots?source=rss----a51c592225f3---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Angular Shots - Medium</title>
            <link>https://medium.com/angular-shots?source=rss----a51c592225f3---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 06:42:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/angular-shots" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Shot: How to get route data]]></title>
            <link>https://medium.com/angular-shots/shot-how-to-get-route-data-6e184b826c25?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/6e184b826c25</guid>
            <category><![CDATA[angular-router]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Demeshko Alexander]]></dc:creator>
            <pubDate>Wed, 29 May 2019 08:46:25 GMT</pubDate>
            <atom:updated>2019-06-03T16:52:08.831Z</atom:updated>
            <content:encoded><![CDATA[<h3>💉 Shot #9: How to get route data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzM-96wYRiWpufA9-qdXlA.png" /></figure><h4>Introduction</h4><p>Angular routing configuration allows us to add some extra data to Route declaration. Let’s assume we have PageComponent and routing configuration presented below. Our goal is to get data in component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06631c8129626e05a735c690ab48f449/href">https://medium.com/media/06631c8129626e05a735c690ab48f449/href</a></iframe><h4>Step 1: Inject ActivatedRoute</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8324d4a9835dc60a3b382732821d84a4/href">https://medium.com/media/8324d4a9835dc60a3b382732821d84a4/href</a></iframe><h4>Step 2: Get route data</h4><p>ActivatedRoute has data attribute with type Observable&lt;Data&gt;, if you assume that route data might change, you need to subscribe to it. In most cases route data is static and you can use route snapshot:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/765c7adea8540cec47eeea46bd7db599/href">https://medium.com/media/765c7adea8540cec47eeea46bd7db599/href</a></iframe><h4>Shot</h4><p>Here is the final shot:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c3db38b32841280ab480a64b6335f4c/href">https://medium.com/media/2c3db38b32841280ab480a64b6335f4c/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/guide/router">Routing and Navigation</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e184b826c25" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-how-to-get-route-data-6e184b826c25">Shot: How to get route data</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #8: How to create a structural directive *isAuthorized (custom *ngIf)]]></title>
            <link>https://medium.com/angular-shots/shot-8-how-to-create-a-structural-directive-isauthorized-custom-ngif-c07494e0b538?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/c07494e0b538</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dmitry Nehaychik]]></dc:creator>
            <pubDate>Tue, 14 May 2019 13:14:05 GMT</pubDate>
            <atom:updated>2019-05-14T16:09:39.740Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IFCgQ-6GLitr-7WAtNt33g.png" /></figure><h4>Introduction</h4><p>Angular has quite a powerful template syntax. Moreover, we are able to extend it creating custom structural directives. This gives us the ability to re-use repeated code parts making code cleaner and simpler.</p><p>Let’s assume that we need to show/hide elements based on the user authentication status. For instance, we have a method that checks if the user has logged in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbf4be65055f37823ef355fddf2096d1/href">https://medium.com/media/bbf4be65055f37823ef355fddf2096d1/href</a></iframe><p>Now we can use the isAuthorized variable to check this status in the template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b12fc8ea390375ff0d23746e95a6634/href">https://medium.com/media/5b12fc8ea390375ff0d23746e95a6634/href</a></iframe><p>In case we have these checks in multiple templates, our app will have duplicated code blocks across the app, making it harder to maintain.</p><p>Luckily, we can move these code parts into a separated structural directive and use it directly in the template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a21534de32b50f77af09715db3a046de/href">https://medium.com/media/a21534de32b50f77af09715db3a046de/href</a></iframe><h4>Step 1: Create a structural directive</h4><p>Create a directive class and give it a proper selector:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/493e839234ea54318b1d51eb92920f60/href">https://medium.com/media/493e839234ea54318b1d51eb92920f60/href</a></iframe><h4>Step 2: Inject TemplateRef and ViewContainerRef</h4><p>All magic appears behind the * sign. When Angular meets a directive that starts with * it wraps the whole host element into <em>invisible </em>&lt;ng-template&gt;&lt;/ng-template&gt; element, allowing us to inject the host element into the directive and manipulate it respectively (show or hide, for instance):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7aeb926b3ae91d0875b295e8cc9fa90/href">https://medium.com/media/a7aeb926b3ae91d0875b295e8cc9fa90/href</a></iframe><h4>Step 3: Create/remove the template</h4><p>Now, based on the data returned from userService, we can either remove the template from the output or create it there:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d62b570c0b7e182e965790929d8a534e/href">https://medium.com/media/d62b570c0b7e182e965790929d8a534e/href</a></iframe><h4>Step 4: Register the directive</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/647807c6290f0138a7ff1a09836e8fcd/href">https://medium.com/media/647807c6290f0138a7ff1a09836e8fcd/href</a></iframe><h4>Shot</h4><p>And here is the final shot ready for injection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a69eeba956c99bc05883265056e27d79/href">https://medium.com/media/a69eeba956c99bc05883265056e27d79/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/guide/structural-directives#write-a-structural-directive">Write a structural directive</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c07494e0b538" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-8-how-to-create-a-structural-directive-isauthorized-custom-ngif-c07494e0b538">💉 Shot #8: How to create a structural directive *isAuthorized (custom *ngIf)</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #7: How to implement custom form control in Angular]]></title>
            <link>https://medium.com/angular-shots/shot-7-how-to-implement-custom-form-control-in-angular-367016831daa?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/367016831daa</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Nikita Poltoratsky]]></dc:creator>
            <pubDate>Thu, 02 May 2019 23:57:31 GMT</pubDate>
            <atom:updated>2019-05-02T23:57:31.358Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dtpoi81QV8xh6GMZ85zyQw.png" /></figure><h4>Introduction</h4><p>If we want to use Angular forms bindings with custom form controls we need to implement ControlValueAccessor interface.</p><p>Let’s assume we have a button with a counter that increments each time the user clicks on it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee96a3a7bc47799436556936ba3f18b0/href">https://medium.com/media/ee96a3a7bc47799436556936ba3f18b0/href</a></iframe><p>And we need the ability to use it with the Angular forms API like so</p><pre>&lt;counter-button [formControlName]=&quot;counter&quot;&gt;&lt;/counter-button&gt;</pre><h4>Step 1: Implement ControlValueAccessor interface</h4><p>First of all, we need to implement ControlValueAccessor interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a9786233f52719ff35ee076bd1c331b/href">https://medium.com/media/3a9786233f52719ff35ee076bd1c331b/href</a></iframe><h4>Step 2: Implement writeValue</h4><p>writeValue(obj: any): void is the method which writes value from the data model to our custom control.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50dce53813914632bb2bb1a13c47da77/href">https://medium.com/media/50dce53813914632bb2bb1a13c47da77/href</a></iframe><h4>Step 3: Implement registerOnChange</h4><p>registerOnChange(fn: any): void registers the callback that has to be called to propagate changes back to the model. And then we call registerOnChange when change data model internally in the inc function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9705822115f3c64c6c3ea89b916e13e/href">https://medium.com/media/d9705822115f3c64c6c3ea89b916e13e/href</a></iframe><h4>Step 4: Skip registerOnTouched</h4><p>registerOnTouched(fn: any): void registers the function that has to be called when custom control was touched. In our case we don’t need registerOnTouched, so, let’s skip it. For more information, check the link at the end of the article.</p><h4>Step 5: Register ControlValueAccessor</h4><p>The final step we need to perform is to register CounterButton as a NG_VALUE_ACCESSOR.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aff48f870f0931cd46d55ba07b631d37/href">https://medium.com/media/aff48f870f0931cd46d55ba07b631d37/href</a></iframe><h4>Shot</h4><p>Here is the final shot ready for usage</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3ed1e0f495dd27d503c7ff36da3a84c/href">https://medium.com/media/c3ed1e0f495dd27d503c7ff36da3a84c/href</a></iframe><h4>Useful Links</h4><ol><li>ControlValueAccessor— <a href="https://angular.io/api/forms/ControlValueAccessor">https://angular.io/api/forms/ControlValueAccessor</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=367016831daa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-7-how-to-implement-custom-form-control-in-angular-367016831daa">💉 Shot #7: How to implement custom form control in Angular</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #6: How to apply multiple *ngIf/*ngFor statements to an element]]></title>
            <link>https://medium.com/angular-shots/shot-6-how-to-apply-multiple-ngif-ngfor-statements-to-an-element-2d7eed0034df?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/2d7eed0034df</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dmitry Nehaychik]]></dc:creator>
            <pubDate>Thu, 25 Apr 2019 14:30:24 GMT</pubDate>
            <atom:updated>2019-04-25T14:30:23.935Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_WNDksZHVIzfDUh1MBJyg.png" /></figure><h4>Introduction</h4><p>Have you ever been in a situation when you need to apply *ngFor and *ngIf to a single element? If yes, then you should know that this is unfortunately not possible in Angular.</p><p>Let’s imagine that we need to render a list of product names, but should exclude products that are out of stock:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7efaf7be4cd69618e63a4e594c074e5d/href">https://medium.com/media/7efaf7be4cd69618e63a4e594c074e5d/href</a></iframe><p>In this case, we are adding additional span element, since we cannot have two structural directives at the same element.</p><h4>Shot</h4><p>Fortunately, Angular provides us with a special kind of element, called &lt;ng-container&gt;. It won’t be rendered into the resulting DOM, allowing us to group elements together to apply structural directives to them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7b31362edbd995f4e1bc5d0745542cd/href">https://medium.com/media/e7b31362edbd995f4e1bc5d0745542cd/href</a></iframe><p>This is especially useful, in a case we <em>cannot</em> have additional elements, for example, rendering &lt;option&gt; elements inside of &lt;select&gt; element:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/049e2235aeb3c395c85c8bb3dd9923f7/href">https://medium.com/media/049e2235aeb3c395c85c8bb3dd9923f7/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/guide/structural-directives#group-sibling-elements-with-ng-container">Grouping Elements with ng-container</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d7eed0034df" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-6-how-to-apply-multiple-ngif-ngfor-statements-to-an-element-2d7eed0034df">💉 Shot #6: How to apply multiple *ngIf/*ngFor statements to an element</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #5: How to create custom Pipe in Angular]]></title>
            <link>https://medium.com/angular-shots/shot-5-how-to-create-custom-pipe-in-angular-4518e029abe4?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/4518e029abe4</guid>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular2]]></category>
            <dc:creator><![CDATA[Nikita Poltoratsky]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 16:22:25 GMT</pubDate>
            <atom:updated>2019-04-23T16:22:25.711Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QQey_yi1q0gr7JXswaBBsg.png" /></figure><h4>Introduction</h4><p>We usually use predefined Angular pipes to prepare data for presentation, for example, lowercase and date pipes. But from time to time we need to create custom pipes. Let’s assume we need to create a debug pipe that will console.log all passed values.</p><pre>{{ user.email | debug }}</pre><h4>Step 1: Create a pipe class</h4><p>First of all, we need to create a pipe class and mark it as an Angular pipe.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c16b179a533217b57b83decca35853a/href">https://medium.com/media/1c16b179a533217b57b83decca35853a/href</a></iframe><h4>Step 2: Implement PipeTransform</h4><p>Then we have to implement PipeTransform interface — create the transform method that will receive value, log it and return.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5918db4ac4c81a65af0b04fa11b66ff/href">https://medium.com/media/f5918db4ac4c81a65af0b04fa11b66ff/href</a></iframe><h4>Step 3: Add pipe to declarations</h4><p>The last thing we need to do is to add MyPipe to the module declarations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3348962a8128dda1a2811a83cc17a548/href">https://medium.com/media/3348962a8128dda1a2811a83cc17a548/href</a></iframe><h4>Shot</h4><p>Here is the final shot, ready to use.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/328bfe42f7b17f0fe6f04b07f06e6b72/href">https://medium.com/media/328bfe42f7b17f0fe6f04b07f06e6b72/href</a></iframe><h4>Useful Links</h4><ol><li>Angular Pipes — <a href="https://angular.io/guide/pipes">https://angular.io/guide/pipes</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4518e029abe4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-5-how-to-create-custom-pipe-in-angular-4518e029abe4">💉 Shot #5: How to create custom Pipe in Angular</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #4: How to listen Angular Router events]]></title>
            <link>https://medium.com/angular-shots/shot-4-how-to-listen-angular-router-events-7a102cca5a80?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/7a102cca5a80</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[rxjs]]></category>
            <dc:creator><![CDATA[Nikita Poltoratsky]]></dc:creator>
            <pubDate>Mon, 22 Apr 2019 14:53:38 GMT</pubDate>
            <atom:updated>2019-04-22T14:53:38.432Z</atom:updated>
            <content:encoded><![CDATA[<h3>💉 Shot #4: How to listen to Angular Router events</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0jLzT5EUcQUMW97w9Cd9Uw.png" /></figure><h4>Introduction</h4><p>There is a bunch of cases when you need to listen to Router events in Angular applications, for example, to show navigation loaders or show user navigation errors.</p><h4>Step 1: Inject Router</h4><p>Firstly, we have to inject Router in the component which will listen for events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4725b57299e354e20518f2c5de801feb/href">https://medium.com/media/4725b57299e354e20518f2c5de801feb/href</a></iframe><h4>Step 2: Subscribe on events</h4><p>Then, we subscribe to Router events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e26f1f074287715f57f237814ff13753/href">https://medium.com/media/e26f1f074287715f57f237814ff13753/href</a></iframe><h4>Step 3: Handle type of event</h4><p>Router events stream fires all router events. That’s why we have to use instanceof to filter only required events for instance NavigationStart.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce3aed50c503aba48283ce6fb1f0ce33/href">https://medium.com/media/ce3aed50c503aba48283ce6fb1f0ce33/href</a></iframe><h4>Shot</h4><p>Here is the final shot:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f4d859012325e63fa533a7473fddd55/href">https://medium.com/media/0f4d859012325e63fa533a7473fddd55/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/guide/router">Routing and Navigation</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a102cca5a80" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-4-how-to-listen-angular-router-events-7a102cca5a80">💉 Shot #4: How to listen Angular Router events</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #3: How to add HTTP Headers to every request in Angular]]></title>
            <link>https://medium.com/angular-shots/shot-3-how-to-add-http-headers-to-every-request-in-angular-fab3d10edc26?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/fab3d10edc26</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Dmitry Nehaychik]]></dc:creator>
            <pubDate>Sun, 21 Apr 2019 14:10:59 GMT</pubDate>
            <atom:updated>2019-04-21T14:10:59.116Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nW72tPUgbCocoUiF69kB6w.png" /></figure><h4>Introduction</h4><p>Angular Interceptors technique comes in handy when you need to transform each request data, for instance, send an authorization token header with every HTTP request.</p><p>Let’s assume we have a service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22ab9b3a98d08778b285534364662df0/href">https://medium.com/media/22ab9b3a98d08778b285534364662df0/href</a></iframe><p>We need to add an additional header to every request and don’t want to modify methods or create <em>base HTTP</em> class.</p><h4>Step 1: Create HTTP interceptor</h4><p>HTTP request interceptor is a special kind of service that hooks into the request process and allows us to modify request data:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d181ffefdf6913966ee010f818edc7da/href">https://medium.com/media/d181ffefdf6913966ee010f818edc7da/href</a></iframe><h4>Step 2: Add token to the request</h4><p>Using the intercept() method we need to clone the original request, modify it, and then call next.handle() to pass the modified request:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb690cf3102c3f8c023b2df15db61192/href">https://medium.com/media/cb690cf3102c3f8c023b2df15db61192/href</a></iframe><h4>Step 3: Register the interceptor</h4><p>Your interceptor should be provided using HTTP_INTERCEPTORS injection token:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2cb9ea2c566ebdb1638a487883c6ae97/href">https://medium.com/media/2cb9ea2c566ebdb1638a487883c6ae97/href</a></iframe><h3>Shot</h3><p>And here is the final shot ready for injection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1920bc90b8bc2f98de036fdb0e59768d/href">https://medium.com/media/1920bc90b8bc2f98de036fdb0e59768d/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/guide/http#intercepting-requests-and-responses">Intercepting requests and responses</a></li><li><a href="https://angular.io/guide/http#httpclient">Angular HttpClient</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fab3d10edc26" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-3-how-to-add-http-headers-to-every-request-in-angular-fab3d10edc26">💉 Shot #3: How to add HTTP Headers to every request in Angular</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #2: How to select Component or Directive in Angular template]]></title>
            <link>https://medium.com/angular-shots/shot-2-how-to-select-component-or-directive-in-angular-template-320f8bde3375?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/320f8bde3375</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Nikita Poltoratsky]]></dc:creator>
            <pubDate>Sun, 21 Apr 2019 14:05:10 GMT</pubDate>
            <atom:updated>2019-04-21T14:05:10.260Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvXSNMkbX7Gb_UIPanriww.png" /></figure><h4>Introduction</h4><p>There are a lot of use cases when you need to get a reference of a Component or Directive in Angular template to modify the entity properties or call it’s APIs.</p><p>Let’s assume we have a directive and component where directive applied:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62ffd3b014500ad79bca2ab0f498431a/href">https://medium.com/media/62ffd3b014500ad79bca2ab0f498431a/href</a></iframe><h4>Step 1: Use @ViewChild to select instance of the Directive</h4><p>With the magic of property decorator @ViewChild, we can ask Angular to inject a reference to the MyDirective in a property of our choice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b14be2990c1d6d23e4bccd09dd6bc3dd/href">https://medium.com/media/b14be2990c1d6d23e4bccd09dd6bc3dd/href</a></iframe><h4>Step 2: Use the reference</h4><p>The reference will be available in the ngAfterViewInit hook, since we need to wait until the template is actually rendered to obtain the reference.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bfe4084c7b9a10380f8051baeb0ce4b/href">https://medium.com/media/8bfe4084c7b9a10380f8051baeb0ce4b/href</a></iframe><h4>Shot</h4><p>And here is the final shot ready for injection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2041f91005251bab046425b5c99410e7/href">https://medium.com/media/2041f91005251bab046425b5c99410e7/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/api/core/ViewChild">@ViewChild property decorator</a></li></ol><h4>Related Shots</h4><ol><li><a href="https://medium.com/angular-shots/shot-1-how-to-select-an-element-in-angular-template-929042bea42">How to select an element in Angular template</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=320f8bde3375" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-2-how-to-select-component-or-directive-in-angular-template-320f8bde3375">💉 Shot #2: How to select Component or Directive in Angular template</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Shot #1: How to select an element in Angular template]]></title>
            <link>https://medium.com/angular-shots/shot-1-how-to-select-an-element-in-angular-template-929042bea42?source=rss----a51c592225f3---4</link>
            <guid isPermaLink="false">https://medium.com/p/929042bea42</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Nikita Poltoratsky]]></dc:creator>
            <pubDate>Sat, 20 Apr 2019 10:10:40 GMT</pubDate>
            <atom:updated>2019-04-21T14:03:08.496Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SE4h8RQicB-bfEFea02Fsw.png" /></figure><h4>Introduction</h4><p>There are a lot of use cases when you need to get a reference of an element in Angular template to modify the element properties or call native APIs.</p><p>Let’s assume we have a component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49f4b51f0fece42f936accc489091d34/href">https://medium.com/media/49f4b51f0fece42f936accc489091d34/href</a></iframe><h4>Step 1: Add template reference variable to the element</h4><p>In Angular templates, we have the so-called <em>template reference variable</em> we can assign to get references to elements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a65b93efad3bc901f137a5cc8ca3d40/href">https://medium.com/media/4a65b93efad3bc901f137a5cc8ca3d40/href</a></iframe><h4>Step 2: Use @ViewChild to select the element</h4><p>With the magic of property decorator @ViewChild, we can ask Angular to inject a reference to the element in a property of our choice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d6d137049b5b044577e0a031a218b27/href">https://medium.com/media/7d6d137049b5b044577e0a031a218b27/href</a></iframe><h4>Step 3: Use the reference</h4><p>The reference will be available in the ngAfterViewInit hook, since we need to wait until the template is actually rendered to obtain the reference.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eaf85d0104f85fe7e8b3eea08a1dec45/href">https://medium.com/media/eaf85d0104f85fe7e8b3eea08a1dec45/href</a></iframe><h4>Shot</h4><p>And here is the final shot ready for injection:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/272cf3775a0edb6fa819dddf972620e9/href">https://medium.com/media/272cf3775a0edb6fa819dddf972620e9/href</a></iframe><h4>Useful Links</h4><ol><li><a href="https://angular.io/api/core/ViewChild">@ViewChild property decorator</a></li><li><a href="https://angular.io/guide/template-syntax#expression-context">Template expression context</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=929042bea42" width="1" height="1" alt=""><hr><p><a href="https://medium.com/angular-shots/shot-1-how-to-select-an-element-in-angular-template-929042bea42">💉 Shot #1: How to select an element in Angular template</a> was originally published in <a href="https://medium.com/angular-shots">Angular Shots</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>