<?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[Stories by Zaeem Akhter on Medium]]></title>
        <description><![CDATA[Stories by Zaeem Akhter on Medium]]></description>
        <link>https://medium.com/@zeeakhter18?source=rss-2fb79f7d6b1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m2NUVwgJj_nkis5n</url>
            <title>Stories by Zaeem Akhter on Medium</title>
            <link>https://medium.com/@zeeakhter18?source=rss-2fb79f7d6b1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:42:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zeeakhter18/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Custom Directives In Angular 13]]></title>
            <link>https://medium.com/@zeeakhter18/custom-directives-in-angular-13-d3c5e0c7b931?source=rss-2fb79f7d6b1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3c5e0c7b931</guid>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[custom-directive]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Zaeem Akhter]]></dc:creator>
            <pubDate>Sun, 23 Oct 2022 19:47:13 GMT</pubDate>
            <atom:updated>2022-10-23T19:47:13.617Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tvC9YaDlCPCGnney.png" /></figure><p>Directives manipulates DOM elements, add or remove them and adds additional behavior to elements in your application. We have different types of directives in angular like Component, Structural and Attribute directive. Most commonly used build-in directives are <br><strong>Structural Directives </strong>ngFor, ngSwitch<br><strong>Attribute Directives </strong>ngClass, ngStyle</p><p>In this blog we are going to see how to create custom directives.</p><p><strong>Custom Attribute Directive</strong></p><p>We are going to use HostListener from angular/core. Its a directive which listen’s to event of the element on which this directive will sits on.</p><p>HostBinding is another directive imported from angular/core which we binds from the element property to change its behavior.</p><p>Our directive will change the color of the element on mouse hover.</p><p>We will use property binding on our directive using @Input() parameter so that we can dynamically input values into our directive.</p><p>Our directive will listens for mouse enter and leave events and we will bind the background property of element.</p><figure><img alt="@Directive({ selector: ‘[appFirstCustomAttribute]’ }) export class FirstCustomAttributeDirective implements OnInit { @Input() highlightColor: string; defaultColor: string = ‘transparent’; @HostBinding(‘style.backgroundColor’) backgroundColor: string = this.defaultColor; constructor(private elRef: ElementRef, private renderer: Renderer2) { } ngOnInit(): void { } @HostListener(‘mouseenter’) mouseenter(){ this.backgroundColor = this.highlightColor; } @HostListener(‘mous" src="https://cdn-images-1.medium.com/max/1024/1*VjdcLkSidRKjt2uAtvFZIg.png" /><figcaption>first-custom-attribute.directive.ts</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*F-IU-krCvKpyXe9YtsmlRg.png" /><figcaption>app.component.html</figcaption></figure><p>You can see here we don’t have used square brackets for our appFirstCustomAttribute because in our directive.ts we have already told angular that its going to be attribute directive by adding [] in selector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*EdvEQn9YIEuYsMyd5LdB2Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DbnTbpELwIrjFYyqhbNMjg.png" /><figcaption>browser</figcaption></figure><p><strong>Custom Structural Directive</strong></p><p>We will create a very simple directive which will be opposite to what ngIf is and we will call it appIfNot. It will work contrary of what ngIf do.</p><p>In structural directives * tells the angular that this is structural directive.<br>Why angular needs it? because under the hood angular converts the * syntax into property binding syntax.</p><p>Angular uses &lt;ng-template [ngIf]=”expression” &gt;&lt;/ng-template&gt;</p><p>ng-template is angular provided template which itself does not render in the dom but it render its content in the dom on some condition</p><p>Angular has given us more readable syntax for using structural directive with *</p><p>Creating structural directive we need to inject TemplateRef&lt;any&gt; and ViewContainerRef for creating ng-template under the hood and view inside the DOM respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o1ztm4YNKJRxG1skXCBcBg.png" /><figcaption>if-not.directive.ts</figcaption></figure><p>We need to create same name Input parameter as of directives name since property binding is used behind this * and we need to run a method when expression value changes so create a setter like @Input set directivename(expression) {}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*sKDY5fY1KLmUXs4slkmoRw.png" /><figcaption>app.component.html</figcaption></figure><p>Whenever “even” will be false it will render the above &lt;p&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*FuqdRJ9mPOEDGFwg9gtCeQ.png" /><figcaption>browser</figcaption></figure><p>That’s it!!!</p><p>We’ve learned how to create custom directives in Angular.</p><p>P.S I am learning angular from Maximilian Schwarzmüller udemy angular course.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3c5e0c7b931" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>