Skip to main content

Directives in Angular

Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element that tell Angular to attach a specified behavior to that DOM element or even transform the DOM element and its children. Mostly directives in Angular starts with ng- where ng stands for Angular, and it extends the HTML.

A directive modifies the DOM by changing the appearance, behavior, or layout of DOM elements. Directives just like Component are one of the core building blocks in the Angular framework to build applications. 

There are three kinds of directives:

1. Component Directives

2. Structural Directives

3. Attribute Directives


Component Directives

Components are the most common of the directives. It contains the details of how the component should be processed, instantiated, and used at runtime. The component comprises meta-data.

Structural Directives

 These directives are used to manipulate and change the structure of the DOM elements. Structural directives have a star (*) sign before the directive. Like as,* ngIf*ngFor, and *ngSwitch directive.

  • *ngIf : The *ngIf allows us to Add/Remove DOM Element.
  • *ngSwitch : The *ngSwitch will enable us to Add/Remove DOM element. It is same as the switch statement of C#.
  • *ngFor : The *ngFor directive is used to repeat a part of HTML template once per each item from an iterable list (Collection).

Attributes Directives

Attribute directives are used to change the look and behavior of the DOM elements. For example: ngClassngStyle etc.

  •  NgClass: The ngClass Directive is used to add or remove CSS classes to an element.
  • NgStyle: The ngStyle Directive facilitates you to modify the style of an HTML element using the expression. We can also use the ngStyle Directive to change the style of our HTML element dynamically.

How to Create Custom Directives in Angular ?

Here is the command to create the custom directive in the Angular command line tool –

ng g directive change-color
The above command will generate 2 files, change-color.directive.ts and change-color.directive.spec.ts. And in the process, app.module.ts file is updated as well.

Let’s Create Custom Angular Directive Logic


et’s add the appChangeColor directive in the app.component.html view as shown below –

<div style="text-align:center">
// appChangeColor custom Directive
    <h1 appChangeColor>I got colored by Angular Custom Directive</h1>
  </div>

</div>
Output would be

Comments

Popular

HTML Basic

HTML (Hypertext Markup Language) Basic What is HTML ? HTML invented by Tim Berners-Lee in 1990. HTML stands for hypertext markup language. Web browsers receive HTML documents from a web server or from local storage and render the documents into multimedia web pages .We can create web page with different elements like audio, video, image, tables, link, list & text etc, with CSS & JavaScript. version of HTML There are 6 version of HTML HTML Version Year HTML 1991 HTML 2.0 1995 HTML 3.2 1997 HTML 4.01 1999 XHTML 2000 HTML 5 2014 (Latest version) Latest version of HTML Latest version of html is HTML 5.0 , in latest version of html provided new tags for multimedia support. Now we are creating web page  index.html  How we can create Web page <!DOCTYPE html> < html > < head > < title > This is my First Web Page </ title > </ head > < body > < h1 > My First heading ...

Component in Angular

Component in Angular A component is a basic building block of UI in an Angular. it controls on UI of application.  it is easily reusable. or we can say Components are defined using the @component decorator. A component has a selector, template, style, and other properties, using which it specifies the metadata required to process the component. How to create Component we have angular CLI command of creating an angular component ng generate component component-name in short form, we can write it as ng g c component-name once you hit this command below file will add inside the component directory HTML: it defines user interface which contains HTML/View of Application, directive & data binding CSS: it has the style of the respective component TS: (it is typescript file, we have to import component class inside TS file, it has class, decorator) Spec: The spec files are unit tests for your source files. The convention for Angular applications is to have a .spec.ts ...

this keyword in javascript

Welcome file this keyword is used to point at the current object in the code. For instance: If the code is presently at an object created by the help of the ‘new’ keyword, then ‘this’ keyword will point to the object being created. It has different values depending on where it is used: In a method, this refers to the owner object . Alone, this refers to the global object . In strict mode this refer to the global object . In a function, this refers to the global object . In a function, in strict mode, this is undefined . In an object method , this refers to the current object. In the example, this refers to the employee object. this Alone when this keyword is used alone, not written inside a function or object then it will refer to global object. & In browser window global object will be [object window] In a browser window the Global object is [object Window] : var x = this; this in a Function (Default) ...