O'Reilly logo

Mastering Web Application Development with AngularJS by Pawel Kozlowski, Peter Bacon Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining a directive

Each directive must be registered with a module. You call directive() on the module passing in the canonical name of the directive and a factory function that returns the directive definition.

angular.module('app', []).directive('myDir', function() {
  return myDirectiveDefinition;
});

The factory function can be injected with services to be used by the directive.

A directive definition is an object whose fields tell the compiler what the directive does. Some of the fields are declarative (for example, replace: true, which tells the compiler to replace the original element with what is in the template). Some fields are imperative (for example, link: function(…), which provides the linking function to the compiler.

This table describes ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required