Directives in AngularJS

Considering the fact that I discussed MVC in the introduction, it would be very tempting to structure this tutorial by the AngularJS implementation of models, views and controllers. But there is a construct so fundamental to AngularJS that to not start with it would be an error.

That construct is directives. AngularJS describes itself as “HTML enhanced for web apps.” Directives are what facilitate this enhancement; they extend the capability of HTML elements.

I often find analogies to be a useful tool for explaining abstract concepts, such as directives. Consider a homeowner who is having some building work completed on their house. The team of laborers required to complete any non-trivial building project is varied in role and might include:

  • a bricklayer
  • an outfitter
  • a plumber

The types of capability directives extend HTML elements with can be categorized similarly:

  • structural
  • decorative
  • plumbing

If we keep the analogy going a moment longer, HTML is the house and directives are the team of laborers.

Previously I introduced a sample application that we will build during this tutorial. Let’s build upon that by displaying an article title contained within a plain JavaScript object:

Edit index.html to contain the ng-app, ng-controller and ng-bind directives as follows:

You’ll notice that two .js files have appeared. There is a small amount of JavaScript required to make the article title appear in the <div>s and app.js and controllers.js is where it happens. Before we create them, I want to briefly discuss the three directives introduced in the above snippet.

ng-app

ng-app is a plumbing directive. AngularJS looks for which HTML element has the ng-app directive attached to it and uses it to bootstrap an application. ng-app can be placed on any HTML element, not just <html>, and connects a module (in our case, ‘udemyAdmin’) to it. Modules will be covered shortly.

ng-controller

ng-controller is a plumbing directive. It connects a model and any related behaviors to a branch of the HTML tree made available by the usage of ng-app.

ng-bind

ng-bind is a plumbing directive. It connects a model property or the result of a model behavior to the text content of an HTML element. In our example, the titleproperty and getTitle behaviour is available because ng-bind has been used on a<div> which is a child of <body> (where ng-controller has been used).

Edit the contents of app.js as follows:

In the above snippet, we first create a module called ‘tutocean’. This module is created with no dependencies (denoted by the empty array). Modules allow us to partition different parts of an application anytime we wish; this is useful in larger applications. For example, a full tutsocean blog application might be partitioned as follows:

Edit the contents of controllers.js as follows:

In the above snippet, we first retrieve the ‘tutsocean’ module (angular.module is dual operation based on the number of arguments) and then register a controller within it. $scope is a core AngularJS component available for injection to controllers (we cover injection in a later section of this tutorial). $scope is the model exposed by articleCtrl; we add properties and behaviors we want to be available for use in our HTML.

Although app.js and controllers.js are very small at the moment, they will grow as this tutorial progresses and it is useful to refer to snippets of code by file name.