Pages

Thursday, 13 November 2014

Introduction to Angular Js in HTML5

In this Blog Post we are going to some really a interesting topic i.e Angular Js . Angular is one of the Pretty Good framework to design a single page applications quickly and more flexibly. what are single page applications ? a simple example is Gmail, you can see the Gmail it is a single page only the content can change . When i heard angular js, i see lot of keywords which is really a new keywords i.e is never heard before, when i go through the concepts it is very clearly seen that using angular we can create applications with all complex design.what is Angular ? Angular is a js , which is developed as Framework to build web applications. using angular we can create a web app with data binding in two way, importantly we can manipulate the dom at runtime, It is designed like supporting MVC , MVVM pattern in code standard.In a Single Page Applications we have a single page as container and change the Views based on the requirement. like master page and child page in aspx.In angular we have a cool separation of View and Code Logic, using angular we can do the following things.1. Data Binding2. Routing3. Cahcing4. Ajax/Promises5. Dom manipulation6. Resource Handling7. Directives8. Factory9. Service10. Provider11. Templates12. Controllers13. Testing14. Filters15. ModuleAngular uses the Dependency Injection to invoke the code logic.we have the model-view-controller concept.First download the angular js from the following link.Angular JsAdd the script in the page <script src="angular.min.js" />Let see some basic syntax to use the angular. Here we have to learn some new concept directivesFirst we have to define a module and mention in the single page application. For example now our module name is sampleModule , This module has to be defined as ng-app="sampleModule" in the htmlcreate a separate file sampleModule.js and wrote the following code "ng-app"Examplevar sampleModule = angular.module('sampleModule',[]);Here [] is used to inject the dependents like ['$compile']in HTML ng-app="sampleModule" have to mention<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title> <script src="ang/sampleModule.js"></script></head><body ng-app="sampleModule"></body></html>a page can only have a single Module.So now we have to see the controller. controller are used to write the code logic. and then refer that controller in the HTML, that controller objects can only be scope that element, "ng-controller"JS:***var sampleModule = angular.module('sampleModule',[]);sampleModule.controller('sampleController', function ($scope) {});HTML:********<body ng-app="sampleModule"><div> <div ng-controller="sampleController"> </div></div></body>Here we are injecting the $scope to the controller, to get the scope of the element.Another important directive is "ng-model"ng-model:********* <div ng-controller="sampleController"> <input type="text" ng-model="name" /> {{name}} </div>From the above sample we can see that ng-model directive is used in the input text control, what ng-model directive do, actually it creates a variable in that name in the memory and copies the value of that control to that variable , if you heard about Viewmodel concept before then this ng-model do the same thing hear, when ever there is a change in the variable it will reflects the controls value, or when ever there is a change in the control it will change the value of the variable, Two way bindingFrom this post we can see some of the basic introduction to the angular.