Difference between Directive and Component in AngularJS
Last Updated :
12 Sep, 2022
In this article, we will see what is Directive & Components in Angular JS, along with finding the relevant differences between Directive and Components, with knowing their behavioral characteristics for building powerful Angular JS code.
Directives are introduced with the invention of Angular, as a structural framework for building dynamic web applications. Directives were popular in manipulating the DOM i.e. Document Object Model and creating new custom elements that were part of the DOM. With the upgrade in technology and various modifications in the later versions of Angular(Angular 2 and higher versions, as Angular 2 is itself dependent on the component-based structure). The use of Components is now seen with great interest among the developers. Also, Components are termed as a more simplified version of Directives.
Directives are functions that are invoked whenever AngularJs's HTML compiler($compile) finds it in DOM. It tells to attach a specified behavior to that DOM element (e.g. via event listeners), or even to transform the DOM element and its children.
Syntax:
import {Directive} from 'angular2/angular2';
@Directive({........
........})
Example: This example describes the basic usage of Directive in AngularJS.
JavaScript
import {Directive} from 'angular2/angular2';
@Directive({
selector: "[myDirective]",
hostListeners: {
'click': 'showMessage()'
}
})
class Message {
constructor() {}
showMessage() {
console.log('This is an example of directive'); }
}
<button>GeeksforGeeks</button>
Output:
Geeksforgeeks
This is an example of directive
Components are just a simplified version of a directive. More specifically Components can be called a special kind of directive with templates, which are mainly used for component-based architecture. The Components can be utilized to make the simpler configuration in comparison to the plain directives. It facilitates us to write the component directives that will further be utilized to make it easier to upgrade to Angular. There are some cases where the Components are not beneficial to use, i.e. when we need an advanced directive definition options such as priority, multi-element, or terminal, or simply, when we want the particular directive that is triggered by the specific attribute or CSS class, instead of an element.
Syntax:
import {Component, View} from 'angular2/angular2';
@Component ({........
........ })
@View ({............
...........})
Example: This example describes the basic usage of Component in AngularJS.
JavaScript
import {Component, View} from 'angular2/angular2';
@Component({
selector: 'message'
})
@View({
template: `
<h6>Hello GeeksforGeeks</h6>
<h6>This is an example of component</h6> `
})
class Message {
constructor(public version: string) {}
}
Output:
Hello GeeksforGeeks
This is an example of component
Explanation: In the above examples, we have seen how to write simple Angular code for printing some messages, both using Component, and Directive. In the Component’s example, we have seen that it is mandatory to write view property, whereas, for Directives, we do not need to use view or templates.
Difference between Directive & Components in AngularJS:
Components | Directives |
---|
The Component is always an element (‘E’). Templates are the mandatory property and always required in Component.
| The directive can be an attribute, element name, comment, or CSS class (‘E’, ‘A’, ‘C’, ‘M’). The directive doesn’t always require templates.
|
The Component is used to break up the application into smaller components. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model.
| The Directive is used to design reusable components, which are more behavior-oriented.
|
As the Component has views, viewEncapsulation can be defined.
| The Directive doesn’t have views. So you can’t use viewEncapsulation in the directive.
|
Although the Components make it easier to write simple, effective code, it has a simpler configuration than plain directives, it is optimized for component-based architecture. A Component does not support “compile” and “pre-link” functions.
| The directives can be utilized for manipulating DOM objects.
|
Components should never modify any data or DOM that is out of their own scope.
| Directives have isolated scopes, by default the child inherits the scope from its parent.
|
Only one component can be present per DOM element.
| There can be more than one directive in a DOM element,
|
To register components, we use @Component meta-data annotation.
| For directive, we use @Directive meta-data annotation.
|
Conclusion: A basic study on the topic has been given. Whether to use Component or Directive, totally depends on the purpose of writing a particular code for the usage. With the advent of emerging technologies, most web page design architectures are aiming toward obtaining a component-based model and this is when we need Components. On the other hand, directives give us plenty of options as selectors, input, output, and providers to write the code creatively.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read