AngularJS Custom-Directives restrict option guide

AngularJS Directive’s restrict key defines how a directive can be used in HTML. In this post, we will see all possible configuration option using restrict.

This post is a part of AngularJS Directives Tutorial Series.

In previous post, our directive was created to be used as an attribute of an existing element, like <div item-widget> which is the default behavior. But We can choose the way our directive should be used in HTML, using restrict key of Directive Definition Object. The possible values for restrict (and so the ways in which we can use our directive) are:

  • A : Specifies that Directive will be used as an attribute, like <div item-widget></div>, as was done in last example. This is also the default behavior if restrict is not declared. Preferred if just modifying the behavior of existing elements.
  • E : Specifies that Directive will be used as an Element. Like <item-widget></item-widget>. Preferred if creating new content.
  • C : Specifies that Directive can be used as class name in existing HTML elements. Like <div class=”item-widget”></div>
  • M : Specifies that Directive can be used as HTML comments. Like <!– Using directive: item-widget–>

These keys can be combined. Best practices suggest to use only A & E types.

Now if in above example, we were to use item-widget as an Element, we would just declare restrict key with following values:

			.directive('itemWidget', [function() {

Do note that once you declare restrict key, you are fixing it. That means if you will try to use item-widget as attribute, AngularJS will throw an error. If we want to allow item-widget to use both as Element & Attribute, we can do so using following configuration:

			.directive('itemWidget', [function() {

Thanks to above configuration, now our directive can be used both as <item-widget></item-widget> & <div item-widget></div >. No additional changes.

Live Example:

Complete Code:

		<title>Directive Demo</title>  
		<link rel="stylesheet" href="" rel="stylesheet"/>
	<body class="jumbotron container" ng-app="myApp">
		<div ng-controller="AppController as ctrl">
			<h3>List of Sale Items</h3>
			<div ng-repeat="item in ctrl.items">
      <script src="">
			angular.module('myApp', [])
			.controller('AppController', [function() {
              var self = this;
			  self.items = [
							{name: 'Computer', price: 500, condition:'New',brand : 'Lenovo', published:'01/11/2015'},
							{name: 'Phone', price: 200, condition:'New',brand : 'Samsung', published:'02/11/2015'},
							{name: 'Printer', price: 300, condition:'New',brand : 'Brother', published:'06/11/2015'},
							{name: 'Dishwasher', price: 250, condition:'Second-Hand',brand : 'WhirlPool', published:'01/12/2015'},
			.directive('itemWidget', [function() {
					restrict: 'EA'

Shown below is the directive’s content[saleItem.html].

<div class="panel panel-default">
	<div class="panel-heading">
		Published at:<span ng-bind="item.published | date"></span>
	<div class="panel-body">
			Name:<span ng-bind=""></span>
			Condition:<span ng-bind="item.condition"></span>
			Price:<span ng-bind="item.price | currency"></span>
			Brand:<span ng-bind="item.brand"></span>

That’s it for restrict option. Let’s move to next configuration option [scope] of Directive Definition Object, which helps us make reusable directives.

Next Topic : AngularJS Directive’s scope option

Please note that if your AngularJS application is using templateUrl to access HTML partials, you will need an HTTP server on front to serve them. This is due to the fact that Browser’s does not allow serving or requesting files on the file:// protocol.

You can use popular Apache HTTP server to serve files. If you need assistance setting-up server,
this post can help you.

Download Source Code


If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help me improve further our learning process.

If you appreciate the effort I have put in this learning site, help me improve the visibility of this site towards global audience by sharing and linking this site from within and beyond your network. You & your friends can always link my site from your site on, and share the learning.

After all, we are here to learn together, aren’t we?