import { Directive, ElementRef, Renderer } from '@angular/core'; @Directive({ selector: '[stToday]', // attribute to be used }) export class TodayDirective { constructor( element: ElementRef , renderer : Renderer) { var today = new Date().toDateString() renderer.setElementProperty(element.nativeElement,'innerText', today) // channge innerText property of the element } }
import { Component } from '@angular/core'; @Component({ selector: 'st-use-custom-directive', templateUrl : 'user-custom-directive.component.html' }) export class UseCustomDirectiveComponent { }
<h1 stToday></h1>
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { TodayDirective } from './today.directive'; import { UseCustomDirectiveComponent } from './use-custom-directive.component'; @NgModule({ declarations: [ UseCustomDirectiveComponent, TodayDirective ], imports: [ BrowserModule ], providers: [], bootstrap: [UseCustomDirectiveComponent] }) export class AppModule { }
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Custom Directives</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <st-use-custom-directive></st-use-custom-directive> </body> </html>
import { Directive, ElementRef, Renderer, HostListener, Input } from '@angular/core'; @Directive({ selector: '[stHighlight]', }) export class HighlightDirective { orgSize : string; orgColor : string; // Receive input - color and size - from client @Input('size') size : string = '20pt'; @Input('color') color : string = 'red'; constructor( private element: ElementRef , private renderer : Renderer) { // preserve original color and font-size this.orgSize = element.nativeElement.getAttribute("font-size") this.orgColor = element.nativeElement.getAttribute("color") } // Listen to event of the client - mouseenter @HostListener('mouseenter') onMouseEnter() { // set new color and font-size this.renderer.setElementStyle(this.element.nativeElement, 'font-size',this.size); this.renderer.setElementStyle(this.element.nativeElement, 'color',this.color); } // Listen to event of the client - mouseleave @HostListener('mouseleave') onMouseLeave() { // restore original color and font-size this.renderer.setElementStyle(this.element.nativeElement, 'font-size',this.orgSize); this.renderer.setElementStyle(this.element.nativeElement, 'color',this.orgColor); } }
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { TodayDirective } from './today.directive'; import { HighlightDirective } from './highlight.directive'; import { UseCustomDirectiveComponent } from './use-custom-directive.component'; @NgModule({ declarations: [ UseCustomDirectiveComponent, TodayDirective, HighlightDirective ], imports: [ BrowserModule ], providers: [], bootstrap: [UseCustomDirectiveComponent] }) export class AppModule { }
<pre class="code"> <h1 stToday></h1> <div stHighlight size="20pt">Srikanth Technologies</div> <div stHighlight color="green" size="30pt">Srikanth Pragada</div>