Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Directives in angular

What is directives and types of directives in angular?

Aman Jain, September 1, 2021February 8, 2024

Directives in angular are custom tags and custom attributes of html which is controlled by class and decorators. Using directives we can change the behaviour, style and logic of a specific area.

Example:

helloworld.component.ts

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

@Component({
  selector: ‘hello-world’,
  template: ‘Hello world component/directive’
 
})
export class HelloWorld implements OnInit {

 constructor() {

   
  }
  ngOnInit() {

  }
}

Xyz.compenent.html

<hello-world></hello-world>

Output:
Hello world component/directive

Types of Directives in Angular:

  1. Component 
  2. Attributes Directive
  3. Structural Directive

1. Component :

Angular page is group of components, we use component everywhere in the angular project. so, we can say that components are main part of an angular application.
Component contains decorators and class. Decorator contains the details of html template, css, change detection strategy and the component selector name.

Let’s create a component using ng generate command

ng g component helloworld

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

@Component({
  selector: ‘hello-world’,
  template: ‘Hello world component/directive’
 
})
export class HelloWorld implements OnInit {

 constructor() {

   
  }
  ngOnInit() {

  }
}

2. Attribute Directive

Attribute directive is used the change the behaviour of element, component or directive. there is some in-build structural directive ngStyle, ngClass, ngModel.

Example:

<div [ngClass]="isActive ? 'active' : ''">This div is active container</div>

3. Structural Directive

Structural directives are directives which add or remove the elements from the dom.

There is 3 in-built structural directives in angular.

  1. ngFor
  2. ngIf
  3. ngSwitch

Example:

<hello-world *ngIf="isActive" [post]="post"></hello-world>

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

Javascript Laravel Multi Select Tag Autocomplete Using Select2

Laravel Multi Select Tag Autocomplete Using Select2

July 19, 2022July 19, 2022

In this article we will learn to use Laravel Multi Select Tag Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. In this article we will cover multiple…

Read More
Javascript How to Insert script tag dynamically in html in angular

How to Insert script tag dynamically in angular ?

March 24, 2022August 4, 2022

Sometimes we want to insert script tag in angular app, For an instance we have third party APIs or widget and we wanted to add it in our page then in this condition provided script are not angular friendly or we can say it can be pure JavaScript. so to…

Read More
Javascript How to create global variable in angular ?

How to create global variables in angular ?

October 3, 2022March 16, 2024

In this article, we will learn about the create global variables in Angular and how to use them. We will also see how to use them across the application by importing the file. There are different ways to create global variables in angular. One way is to use the global…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version