in Technology by
What are directives in Angular?

1 Answer

0 votes
by

A directive is a class in Angular that is declared with a @Directive decorator.
Every directive has its own behaviour and can be imported into various components of an application.

When to use a directive?
Consider an application, where multiple components need to have similar functionalities. The norm thing to do is by adding this functionality individually to every component but, this task is tedious to perform. In such a situation, one can create a directive having the required functionality and then, import the directive to components which require this functionality.

Types of directives
Component directives
These form the main class in directives. Instead of @Directive decorator we use @Component decorator to declare these directives. These directives have a view, a stylesheet and a selector property.

Structural directives
These directives are generally used to manipulate DOM elements.
Every structural directive has a ‘ * ’ sign before them.
We can apply these directives to any DOM element.

Let’s see some built-in structural directives in action:

    
      <div *ngIf="isReady" class="display_name">
          {{name}}
      </div>


      <div class="details" *ngFor="let x of details" >
          <p>{{x.name}}</p>
          <p> {{x.address}}</p>
          <p>{{x.age}}</p>
      </div>
    
  

In the above example, we can *ngIf and *ngFor directives being used.

*ngIf is used to check a boolean value and if it’s truthy,the div element will be displayed.

*ngFor is used to iterate over a list and display each item of the list.

Attribute Directives

These directives are used to change the look and behaviour of a DOM element. Let’s understand attribute directives by creating one:

Related questions

0 votes
    I need to use recursion in angular directives. Follow the code with the template without recursion. It is a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    Which of the following directives connects the value of the controls to the data? 1. ng-model 2. ng-app 3. ng-init 4. None of the above...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    Which among the below directives belonging to the Cache-Control header of HTTP response provide information to the server that the ... -revalidate 3. no-cache 4. None of the above...
asked Jun 24, 2021 in Technology by JackTerrance
0 votes
0 votes
    What are the Components, Modules and Services in Angular?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    What are the differences between AngularJS and Angular?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    What are the advantages of Angular over other frameworks?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    How are observables different from promises in Angular?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    How are Angular expressions different from JavaScript expressions?...
asked Jun 30, 2021 in Education by JackTerrance
0 votes
    What is MVVM architecture in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    How does one share data between components in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    What is string interpolation and property binding in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    Explain about the lifecycle hooks in Angular? Explain a few lifecycle hooks...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    How does an Angular application work?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    What is NgModules in Angular 8?...
asked Jun 28, 2021 in Technology by JackTerrance
...