What is Directive?
- Directives play an important role in Angular 2 projects, when we displaying the templates or html pages in our project. By using this we can easily manipulate our Dom layout.
- In Angular 2, there are three types of directives those are component directive, attribute directive and structural directive
Classification of Directives

What is Component directive?
- Component directive, is nothing but a simple class which is decorated with the @component decorator.
- In Angular 2 ,Normal typescript class will become a Component class once it has been decorated with @component decorator
- It is mainly used to specify the html templates.
- It is most commonly used directive in angular project
- If you need more info on angular component, then kindly refer here: need to add
Built In Component Directive @component
@component decorator provides additional metadata that determines how the component should be processed, instantiated and used at runtime
Simple Example
@component
File name app.component.ts
- import { Component } from "@angular/core";
- //decorator
- @Component({
- selector: 'my-App',
- template: '<h1>{{name}}</h1>'
- })
- export class AppComponent {
- name: string = "Angular 2"
- }
File name index.html
- <!DOCTYPE html>
- <html>
- <head>
- <title>Angular QuickStart</title>
- <base href="/src/">
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="styles.css">
- <!-- Polyfill(s) for older browsers -->
- <script src="/node_modules/core-js/client/shim.min.js"></script>
- <script src="/node_modules/zone.js/dist/zone.js"></script>
- <script src="/node_modules/systemjs/dist/system.src.js"></script>
- <script src="systemjs.config.js"></script>
- <script>
- System.import('main.js').catch(function(err){ console.error(err); });
- </script>
- </head>
- <body>
- <!--Here is the selector mapped-->
- <my-app>Loading AppComponent content here ...</my-app>
- </body>
- </html>
Output

What is attribute directive?
- It is mainly used to change/modify the behavior of the html element.
- As the name tells, it is used to change the attributes of the existing html element. In Angular 2 there are many built in attribute directives. Some of the useful ones are NgClass, NgStyle
Built-In Attribute Directive: NgStyle, NgClass
NgStyle
NgStyle directive is similar to one of data binding technique called style binding in angular, but the main difference is, NgStyle used to set multiple inline styles for html element.
NgClass
It is similar to NgStyle attribute but here it is using class attribute of the html element to apply the style.
Simple Example
NgStyle
File Name app.component.ts
- import { Component } from '@angular/core';
- @Component({
- selector: 'my-app',
- template: ` <button style='color:blue' [ngStyle]="ApplyStyles()">Style Applied</button>
- `
- })
- export class AppComponent {
- isBold: boolean = true;
- fontSize: number = 30;
- isItalic: boolean = true;
- ApplyStyles() {
- let styles = {
- 'font-weight': this.isBold ? 'bold' : 'normal',
- 'font-style': this.isItalic ? 'italic' : 'normal',
- 'font-size.px': this.fontSize
- };
- return styles;
- }
- }
Output
Simple Example
NgClass
File Name app.component.ts
- import { Component } from '@angular/core';
- @Component({
- selector: 'my-app',
- template: `
- <button class='colorClass' [ngClass]='applyClasses()'>Style Applied Using Class</button>
- `,
- styles: [`
- .boldClass{
- font-weight:bold;
- font-size : 30px;
- }
- .italicsClass{
- font-style:italic;
- }
- .colorClass{
- color:grey;
- }
- `]
- })
- export class AppComponent {
- applyBoldClass: boolean = true;
- applyItalicsClass: boolean = true;
- applyClasses() {
- let classes = {
- boldClass: this.applyBoldClass,
- italicsClass: this.applyItalicsClass
- };
- return classes;
- }
- }








Karthik ElumalaiPosted Oct 26, 2017, 12:17 AM
Thank you very much for your valuable feedbackArvind Singh Baghel
Arvind SinghPosted Oct 26, 2017, 12:09 AM
Good one................