Creating Reusable Components with Directives in AngularJS

AngularJS provides a powerful feature called directives that allow you to create reusable components in your application. Directives are a way to extend HTML with new elements or attributes, giving you the ability to create custom behaviors and features. In this article, we will explore how to create reusable components using directives in AngularJS.

What are Directives?

In AngularJS, directives are markers on a DOM element that tell AngularJS's HTML compiler ($compile) to attach a specific behavior or functionality to that element. Directives are defined using the directive function provided by the angular module.

Creating a Simple Directive

To create a directive, you need to define a JavaScript object that represents the directive and register it with AngularJS. Here is an example of a simple directive that adds a custom greeting message to an element:

angular.module('myApp').directive('greet', function() {
  return {
    restrict: 'A',
    link: function(scope, element, attrs) {
      element.text('Hello, ' + attrs.greet);

In this example, we have defined a directive called greet using the directive function. It takes an anonymous function as its second argument, which returns an object representing the directive configuration. The restrict property is set to 'A', which means the directive will be used as an attribute. The link function defines the behavior of the directive by manipulating the element it is attached to.

To use this directive in our HTML, we simply add the greet attribute to an element and provide a value for it:

<div greet="John"></div>

When the page is rendered, the directive will set the text content of the <div> element to 'Hello, John'.

Creating Reusable Components

Directives can be used to create reusable components in your AngularJS application. Instead of writing the same HTML and JavaScript code multiple times, you can encapsulate the functionality into a directive and reuse it throughout your application.

Let's say we want to create a reusable HTML component for displaying a user profile. We can define a directive called userProfile that encapsulates the functionality for displaying user information:

angular.module('myApp').directive('userProfile', function() {
  return {
    restrict: 'E',
    templateUrl: 'user-profile.html',
    scope: {
      user: '='

In this example, we have defined a directive called userProfile that will be used as an element (restrict: 'E'). We have also specified a template URL (templateUrl) that AngularJS will load and use as the HTML for the directive. The scope property defines an isolate scope for the directive, which means it has its own scope separate from the parent scope. We have also declared a two-way data binding (user: '='), which allows us to pass in a user object to the directive.

To use this directive, we can include it in our HTML like this:

<user-profile user="currentUser"></user-profile>

This will render the user profile component using the template specified in the directive. The user attribute binds the currentUser object from the parent scope to the user property of the directive's isolate scope.


Directives are a powerful feature in AngularJS that allow you to create reusable components in your application. By encapsulating functionality into directives, you can easily reuse code and create modular and maintainable applications. In this article, we have seen how to create simple directives as well as how to create reusable components using directives in AngularJS. Start using directives in your AngularJS projects to create powerful and reusable components!

© NoobToMaster - A 10xcoder company