- Getting started with AngularJS
- Creating an AngularJS Controller
- The AngularJS $scope is not the MVC Model
- Using repeating elements in AngularJS
- Filtering the data in an AngularJS ngRepeat element
- Using the AngularJS FormController to control form submission
- Creating an AngularJS Directive
- Using the DOM in an AngularJS application
- To SPA or not to SPA
One of the more powerful aspects of AngularJS are directives. So far we have used a few build in directives but we are by no means limited to the build in ones. In fact creating your own directives is highly recommended as that can turn HTML into a domain specific language if you want that. Now starting with directives has a bit of a learning curve but that is certainly worth the effort!
What is an AngularJS directive?
The easies way to think about a directive is as a reusable piece of markup. Just as we create functions when programming to create reusable pieces of code and we create reusable CSS styles we would like to create reusable HTML parts. Unfortunately HTML itself doesn’t allow us to do that so we normally end up with copying a lot of markup with no reuse making the whole thing a lot less maintainable.
With AngularJS we can use directives to create these reusable pieces of HTML and make our application markup just as maintainable as our code and style sheets.
An example using Bootstrap 3
These days Bootstrap is pretty much the standard CSS framework so lets create a simple input form using this. Creating a simple input form with a horizontal layout is easy enough with just the basic Bootstrap included.
However the markup required for each element is quite verbose resulting in quite a long bit of markup for this simple form. This is because each input element/label combination takes up 6 lines of markup. And worse of all is that they are mostly identical.
Creating the same input form using an AngularJS directive
Once we have created the appropriate AngularJS directive we can shrink the HTML markup to just the code below. I don’t know about you but I most certainly prefer to maintain the markup below 🙂
Writing the directive
Writing a basic AngularJS Directive is quite easy. There are lots of options but in this case we only need a few basic ones. Once we have defined an AngularJS module we can create a directive by calling the directive function. Because we are not doing anything special all we need to do is provide a function that returns an object that defines the directive.
Directives can be used in a number of ways but this one is used as an HTML element so we need to specify that using the ‘restrict’ property. Additionally we want to provide some variables per instance, in this case the model property to bind to and the label to display in before the textbox. Additionally we display the template which is basically the HTML template we want to insert in place of the directive itself.
This is just a very basic directive, there is a lot more possible. For more info check the documentation.