Thursday, February 18, 2016

AngularJS Directives

Directives provide AngularJS core functionality of extending HTML with new attributes.
There are a lot of built-in directives to support diverse functionality to the applications.

e.g.
  • ng-app directive initializes an AngularJS application.
  • ng-init directive initializes application data.
  • ng-model directive binds the HTML controls to application data.

We can also define our own directives depending on requirements.

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp">

<div my-btn-submit></div>
<my-btn-submit></my-btn-submit>
<div class="my-btn-submit">

<script>
var app = angular.module("myApp", []);
app.directive("myBtnSubmit", function() {
    return {
        restrict : "C",
        template : "<input type='submit' value='Submit'></input>"
    };
});
</script>

</body>
</html>


We can restrict directive invocation with 'restrict' values are:

E - Invoke as Element name
A - Can be used as Attribute
C - Can be use as Class



By default the value is EA, means can be invoked both as, an Element name and attribute name.

AngularJS Modules


AngularJS module defines the application. Which wraps up all different components of the application like controllers, directives etc. The controllers always belong to a module.

<div ng-app="myApp">...</div>

<script>
var app = angular.module("myApp", []);
</script>

AngularJS Expressions

AngularJS Expressions are used to bind data to HTML and can be written in side double curly braces {{2+2}} and in can also be written in directives like ng-bind=”2+2”.

<div ng-app="">
  <p> Angular Expression: {{ 10 + 20 }}</p>
</div>

AngularJS Expressions are very similar to JavaScript Expressions but there are some differences.

  • Both support literals, operators, and variables.
  • Angular expressions support filters, while JavaScript expressions do not.
  • Angular expressions can be written inside HTML but we can not write JavaScript expressions inside HTML.
  • JavaScript expressions support conditionals, loops, and exceptions, while Angular expressions do not support this.


What is difference between angular expression {{}} and ‘ng-bind’ and which is better?

The expression {{}} binds data to HTML same like ng-bind directive. But ng-bind is better approach than {{}}. Followings are few of the considerations.


  • Key difference is {{ }} can flash on page load while ‘ng-bind’ hides the expression until it is displayed correctly.
  • The Angular JS registers a watcher for the variable being used in ng-bind.
  • The ‘ng-bind’ stores only value of the variable in memory.

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

    <div ng-app="" data-ng-init="var1=5;var2=7">

        <p>Result = <span data-ng-bind="var1+var2"></span></p>
        <div>Result =  {{(var1+var2)}}</div>

    </div>

</body>
</html>

Initializing multiple variables in ‘ng-init’ for AngularJS application

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="" data-ng-init="var1=5;var2=7; varStr='Hello'">
<p> <span data-ng-bind="varStr"></span></p>
<p> var1 = <span data-ng-bind="var1"></span></p>
<p>var2 = <span data-ng-bind="var2"></span></p>
<div>var1+var2 = {{(4+5)}}</div>

</div>

</body>
</html>