Back to Top

How to use Data Binding and Directives in Angular JS

Angular JS - Data Binding and Directives

Here I come with the next article about Angular JS series and it is about data binding and directives in Angular JS. I have explained basics about data binding in the first article also and Today we will go in depth so you can use data binding function of AngularJS very easily and skillfully.

ng-bind {{expression}}:

[sociallocker]
If you have read previous articles of angular JS series, I am sure you are already aware with this code. AngualrJS uses ng-bind for data binding, but we will use the expression {{expression}} instead ng-bind as such an approach is more intuitive to use and easy to remember.

OUTPUT:

AngularJS also provides several other data binding way:

  1. Simple Binding:

Here implements bind a simple addition operation which will show the binding between the ng-model and expression.It is known as data binding in Angular JS.

Let’s understand with the code:

Here we have taken very simple example to give basic idea about data binding.ng-model is mainly for data binding that is inside the input type to bind the value and {{FiledName}} expression within double parentheses which is used to bind data.

Let’s see above code run time:

OUTPUT:

  1. $scope

$scope is an object used to bind the data through ng-model, in fact, so-called data binding is actually bound to the $ scope. Here in the following code, SumController method calculates the sum of two numbers when clicking on a button valued “sum”.

When you type something in the text boxes, a property named item is automatically added to the $scope.

Let’s see the above code run time:

  1. $apply()

We have seen in above mentioned method for data binding using $scope which is actually binding data to the ng-model. Simple clock procedures for updating every second clock values, we are mostly using setInterval method in Javascript whereas Here in Angular JS,we will use the apply method which is also a one of the data binding method.

OUTPUT:

setInterval call updateCounter () will print the current time every second, but the interface is not updated.Here setInterval will call $scope. $apply (updateCounter) will display once every one second of the current time, and update the interface.

Why is this? In fact, calling setInterval method cycle triggered by updateCounter methods.The native of setInterval JavaScript can not change the data monitored, so by $ apply to achieve.

  1. Angular Module

.net developers all know that in the development, will declare different namespaces and different classes to implement a modular management code. Such like Angular JS also offers a similar approach with ng-controller,ng-app and module managed code using the angular.module.

angular.module registered by a module in the background, and then to add the controller by app.controller. Here In following code we will use the two methos called ‘SumController‘ and ‘MultiplyController‘ which belongs to same application and $scope object bound the two functions.

OUTPUT:

  1. ngBindHtml:

ngBindHtml (ng-bind-html) can be a string in a secure manner and is inserted into the page displayed as Html.

Let’s check the syntax used for ng-bind-html:

SYNTAX:

ngBindHtml will enforce the use of angular-sanitize service to perform safety checks and it is not included in the core library, you need to include angular-santitize.js external file and add the definition ngModule dependence for ngSantitize statement.

OUTPUT:

As you can see in above code,’ngSanitize’ is passed with angular.module as the second parameter as ngModule dependent on the ngSanitize when using the ng-bind-html.

  1. ngBindTemplate:

OUTPUT:

ngBindTemplate (ng-bind-template) differs from the ngBind. ngBind is only a single bind variables and the variable without using double parentheses “{}”, while ngBindTemplate can bind a template and the template can include multiple AngularJS expressions with using double parentheses “{{expression}}”.

  1. ngNonBindable:

However, if you have a content on your page with double parentheses like “{{my content}}” so such content will be compiled and replaced with value in AngularJS and using ngNonBindable (ng-non-binable), AngularJS will automatically ignore the content.

  1. Twoway Binding using ngModel:

OUTPUT:

The above code shows AngularJS usage of two-way binding.Here We have used Edit, Save and Delete button which will work without any business logic. As it is Two-way binding,so when you enter the data into input and submit data,it will display data to the desired position using the $scope object which bind the value and amazing part is there is no different coding for Edit and Save.

Here we have used some new directives like ng-repeat,ng-show, and ng-hide.Let’s understand all of them below:

ng-repeat SYNTAX:

The ngRepeat directive is used when you’ve multiple values like array, object and you want to display as the loop.

ng-show/ng-hide SYNTAX:

The ngShow directive is used to shows or hides the given HTML element.Here change the ng-show to ng-hide to hide the element.

As we have seen above, two-way data binding makes CRUD functionality very simple using Angular JS and without any business logic. Hope you are now clear with the data binding and directives in Angular JS.if you are not, copy the above examples, run and learn it :).We will explore other features of Angular JS in future series.

If you are unclear with above explanation or any query, write on the ​comment section. Where are you​ using Directives in AngularJS code? Share your knowledge 🙂
[/sociallocker]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Cursor in IndexedDB API HTML5

Posted on 8 years ago

Bhumi

Buffered Vs Unbuffered Queries in PHP

Posted on 12 years ago

Bhumi

How to check user is logged in Modx

Posted on 11 years ago

Bhumi