Attaching behavior to DOM elements with directives

In the course of creating applications, you will often find it useful to be able to attach component-style behavior to DOM elements, but without the need to have templating. If you were to attempt to construct an Angular 2 component without providing a template in some way, you will meet with a stern error telling you that some form of template is required.

Here lies the difference between Angular 2 components and directives: components have views (which can take the form of a template, templateUrl, or @View decorator), whereas directives do not. They otherwise behave identically and provide you with the same behavior.


The code, links, and a live example of this are available at ...

Get Angular 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.