Learn Directive Types with Code



 app.component.html

<ul>
    <li>
      <a routerLink=""> Home</a>
    </li>
    <li>
      <a [routerLink]="">About</a>
    </li>
    <li >
      <a [routerLink]="">Contact</a>
    </li>
    <li>
      <a [routerLink]="">Life Management</a>
    </li>
    <li>
      <a [routerLink]="">AIR News</a>
    </li>
    <li>
      <a [routerLink]="">Essay Writing</a>
    </li>
    <li >
      <a>
        <input type="text" placeholder="Search..">
      </a>
     
    </li>
  </ul>
  
  <h1 class="text-center" > Directive</h1>
  <p style="margin: 2px;">
    There are two types of directives in angular
  </p>
  <div class="row">
    <div class="col-md-6"  [ngClass]="{'warning' : true}">
      <pre [ngClass]="{'success' : true}">
        1. Built-in Directive
        2. Custom Direcitves ( ng generate directive emoji_dir_name)
      </pre>
      <div [ngClass]="{'error': info.priority > 10}">{{info.text}}</div>
      <h1>Build-in Directive</h1>
      <p> There are 3 types of directives in angular which is know as built-in directives</p>
      
      <pre>
        1. component -- Directives with template
        2. Attribute --- [ngStyle] and [ngClass]
        3. Structure ---  *ngIf and *ngFor
      </pre>
      
    </div>
    <div class="col-md-6" [ngClass]="{'warning' : true}">
      <h1 [ngClass]="{'error' : true}">component Directives</h1>
      <p>component directives are the directive with template.</p>
      
      <h1 [ngStyle]="{'background' : isBlue ? 'blue' :'red'}">Attribute Directive</h1>
      <p>Attribute directive are the responsible for manipulating the appearance and behaviour of DOM.</p>
      <pre>
        a. [ngStyle]
        b. [ngClass]
      
      </pre>
    </div>
  </div>
 
  
  <div class="row">
    <div class="col-md-6 text-center">
      
  <h1 *ngIf="show" >Structural Directives</h1>
  <input type="submit"  value="*ngFor iterate country name from list">&nbsp;&nbsp;
  <input type="submit"value="Click *ngIf Effect" (click)="activeStructureDirectiveNgIf()">
  <p> Structural Directives are responsible for changing DOM</p>
  
  
    </div>
    
    <div class="col-md-6">
      <h1 appHeighlight> Custom directive!, Country :</h1>
  
      <select >
        <option  [value]="country" *ngFor="let country of countries">{{country}}</option>
      </select>
    </div>
    
  </div>


app.component.ts 

import { Component,OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'hello';
  isBlue:boolean = true;
  show:boolean = true;
  info:any = {  priority:11,
    text : 'How to add classes dynamically based upon certain condition'
  }
 
  countries = ['INDIA','USA','CANADA','UK','US']
  value = ['value','USA','CANADA','UK','Value']
  ngOnInit(){
  this.countries.push('xyz')
  this.countries.pop()
  this.countries.pop()
let hello = this.countries.toString()

  }
  activeStructureDirectiveNgIf(){
    
    if(this.show == true){
      this.show = false;
    }else{
      this.show = true
    }
  }
}


app-heighlight.directive.ts

// we are going to highlight the selected DOM element by setting an element’s background color. by using custom directive

import { DirectiveElementRef } from '@angular/core';
 //first we are importing Directive and ElementRef from Angular core.
 //  Directive provides the functionality of @Directive decorator
 //  in which we provide its property selector to appHeighLight so that we can use this selector anywhere in the application.

 //  We are also importing the ElementRef which is responsible for accessing the DOM element.
//Now to get appHeighlight Directive to work, we need to add our Directive to the declarations array in the app.module.ts file.
 @Directive({
    selector: '[appHeighlight]'
})

export class HeighlightDirective {
    constructor(private eleRefElementRef) {
        eleRef.nativeElement.style.background = 'cyan';
    }

}

app.module.ts

import { HeighlightDirective } from './app-heighlight.directive';
declarations: [
    AppComponent,
   
    HeighlightDirective,
],

app.component.css


.warning {
    backgroundhsl(48100%67%);
    border5px solid hsl(48100%67%);
    colorblack;
  }
  
  .error {
    backgroundhsl(348100%61%);
    border5px solid hsl(348100%61%);
    colorwhite;
  }
  
  .success {
    backgroundhsl(14171%48%);
    border5px solid hsl(14171%48%);
    colorwhite;
  }

  .message{
    color:green;
}

ul {
    displayinline-flex;
    widthauto;
    overflowhidden;
  background-color#333;
  positionfixed;
  bottom0;
  width100%;

   
}
ul li{
    list-style-typenone;
}
ul li a {
    text-decorationnone;
    floatleft;
    displayblock;
    color#f2f2f2;
    text-aligncenter;
    padding14px 16px;
 
    font-size17px;
}
ul li a:hover{
    background#ddd;
    colorblack;
  
}

.topnav {
    overflowhidden;
    background-color#e9e9e9;
  }

Directives are the instructions in the DOM, they are specify how to place your components and business logic in the angular. They are denoted as @directive


Component directives are used in main class. They contain the details of how the component should be processed, instantiated and used at run time.


Structure directives start with * sign. These directives are used to manipulate and change the structure of the DOM element. e.g. -- *ngIf and *ngFor

Structure directive will affect the whole area in the DOM , start with * symbol and look differnet.


Attribute directives are used to change the look and behaviour of DOM elements. 

eg. ngClass, ngStyle, etc

attribute directive will affect only the element they are added to. They are look like a normal HTTP attribute and mainly used in data binding and event binding.



Comments

Popular posts from this blog

Become a Expert + Secret of Success -- ii

Change Detection

without writing media queries How to make responsive component