Quick n dirty – Angular Directives

13th January 2015 - Angular, QuickAndDirty

Going to be doing a few of these Quick n Dirty tutorials over the coming weeks, showcasing bite size quick how-to’s on useful web tech and IT.

First up, some Angular click bait help for those who want to get stuck into the popular Javascript framework.

The Angular mantra is all about not touching that DOM with your controller, and the best way of following that rule (and create re-usable controls) is by separating as much as you can in Directives.

Directives are components which have their own scope and can be slotted into any page to show content, either by using your very own tag, or including them as an attribute.

Firstly, create an angular app.

<html ng-app="QADApp">
<head><title>QAD Angular directives</title></head>
<!-- (A) html goes here -->

<script src=""></script>
var QADApp=angular.module("QADApp",[]);
// (B) your directive script goes here


Now, lets plan our Directive quickly.

It will show a message, based on a value.  Done.

We will use the Element style of directive by using the following HTML. note how we separate our name by the camel case in the directive configuration.

<my-message-is-shown value="bye"></my-message-is-shown>

..alongside an “E” setting in the directive’s restrict object parameter..
var QADApp=angular.module("QADApp",[]);
<span style="line-height: 1.5;">   restrict:"E",
</span><span style="line-height: 1.5;">   scope:{
</span><span style="line-height: 1.5;">          value:"@"
</span><span style="line-height: 1.5;">   },
</span><span style="line-height: 1.5;">   template:"&lt;div ng-if=\"value=='hello'\"&gt;Howde&lt;/div&gt; &lt;div ng-if=\"value=='bye'\"&gt;See ya&lt;/div&gt;"
</span> }

I generally place the template text in a function, so the code’s readable, but that isn’t quick, or dirty!

The full page, just to show you don’t need a controller either.

&lt;html ng-app="QADApp"&gt;
&lt;head&gt;&lt;title&gt;QAD Angular directives&lt;/title&gt;&lt;/head&gt;
&lt;!-- stuff goes here --&gt;
&lt;<strong>my-message-is-shown</strong> <span style="color: #008080;"><strong>value</strong></span>="bye"&gt;&lt;/<strong>my-message-is-shown</strong>&gt;

&lt;script src=""&gt;&lt;/script&gt;
var QADApp=angular.module("QADApp",[]);

     <span style="color: #008080;"><strong>value</strong></span>:"@"
 template:"&lt;div ng-if=\"<span style="color: #008080;"><strong>value</strong></span>=='hello'\"&gt;Howde&lt;/div&gt; &lt;div ng-if=\"<span style="color: #008080;"><strong>value</strong></span>=='bye'\"&gt;See ya&lt;/div&gt;"


The value attribute can be called anything as it is in its own scope, so I could have done the following;
scope:{ value:"@greeting"}

and changed the attribute name to greeting and it still would work.

Have a look at the Angular directives guide for more information.

Angular Directives

You can create a directive in a separate module and then inject it into the original module like below. I won’t show the whole script, you should be able to work it out. 🙂

var QADApp=angular.module("QADApp",["QADDirectives"]);

See you next time!