Angular.js - create reusable HTML widgets with directives

The Angular.js JavaScript MVC framework has a powerful directive mechanism that can be used to create reusable HTML widgets..

Object Partners

What is a directive?

Angular directives are a very powerful component of the Angular framework. They can control how the page is rendered in many different ways. Many of the attributes and tags built in to Angular are directives, such as ‘ng-show’, ‘ng-hide’, ‘ng-repeat’, etc. One way they can be used is to create HTML an widget that you can re-use across different partials.

How to create a simple directive

The Angular.js directive documentation goes into deep technical detail on how directives are implemented. This deep dive could be useful for complex directives, but it can obscure how easy it is to create simple directives. For example, suppose we want a re-usable HTML widget that messages displays to the user. The first thing we’ll do is create the directive:

angular.module('messageDirectives', [])
    .directive('messageList', function(){
      return {
        restrict: 'E',
        scope: false,
        templateUrl: 'partials/widgets/messages.html'
      }
    });

As you can see, the code to define the directive is pretty minimal. The ‘scope: false’ tells Angular to not create a separate scope for the directive and just pass through the scope from the parent controller. And the ‘restrict: E’ says to only allow this directive to be used in an HTML element.

Next, let’s create the template body:

partials/widgets/messages.html:

<div id="messages">
  <div ui-if="errorMessage" id="errorMessageDiv" class="alert alert-error">{{errorMessage}}</div>

  <div ui-if="successMessage" id="successMessageDiv" class="alert alert-success">{{successMessage}}</div>
</div>

Since we set ‘scope: false’ in the directive definition, errorMessage and successMessage are coming from the parent controller’s $scope. The controller code may contain something along these lines:

if (saveSuccessful) {
  $scope.successMessage = "Item saved successfully";
} else {
  $scope.errorMessage = "Error saving item";
}

Finally, to use the new widget we just include this snippet in one of our partials:

<div>
  <message-list></message-list>

  <h2>Create Report</h2>
</div>

And that’s a quick way to create a re-usable HTML widget with Angular.js.

Share this Post

Related Blog Posts

JavaScript

Visualizing Data with D3 Part 1 -- The Basics

August 15th, 2013

Looking at the basic concepts of using D3 to manipulate the DOM.

Object Partners
JavaScript

I Wish I Knew Then What I Know Now -- Life With AngularJS

August 9th, 2013

A list of things that I wish were clearer to me when I started working with AngularJS

Object Partners
JavaScript

Why Does Web Development Take So Long?

May 6th, 2013

Information technology is moving faster all the time. But, if information technology is progressing rapidly, Why does it takes so long to do web development? How do intelligent technologists solve these problems? Share this infographic: http://www…

Object Partners

About the author