Angular 4 - Angular Directives

 

Angular Directives




Directives are used to give instructions to the DOM. Directives specify how to place components and business logic in an Angular application.Directives js class and declared as @directive. There are mainly three directives in Angular.Those are,
  1. Component Directives
  2. Structural Directives
  3. Attribute Directives

Component Directives

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


Structural Directives

Structural directives should start with a * sign. Structural directives are used to manipulate and change the structure of the DOM elements. Example for structural directives are, *ngIf and *ngFor.

Lets see how we can use *ngIf  in our angular application.
eg: in this application initially our server created is false and it shows a no server is created.



when  we click the"Add server" button "oncreateserver()"  method is executed.it changes server created to true.When server created is true *ngIf outputs 'Server is created. Name of the server is this.serverName;




Attribute Directives

Attribute directives are used to change the look and behavior of the DOM elements. Examples for attribute directives are ngClass, ngStyle etc.

eg: In this example I used ngstyle directive to add color to paragraphs.


  • I have used getcolor() methods in component.ts file to define background color.





  • I have used ngstyle directive to add colors to following examples.



Comments

Post a Comment

Popular posts from this blog

Angular 6 - Data Binding Part 2

Machine Learning

Selenium - 1