AngularJS – Maps Application ”; Previous Next We are providing an example of Maps app. To develop this app, we have used HTML, CSS, Maps and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;
Category: angularjs
AngularJS – Weather Application ”; Previous Next We are providing an example of Weather app. To develop this app, we have used HTML, CSS, weather.js and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;
AngularJS – Quick Guide
AngularJS – Quick Guide ”; Previous Next AngularJS – Overview What is AngularJS? AngularJS is an open source web application framework. It was originally developed in 2009 by Misko Hevery and Adam Abrons. It is now maintained by Google. Its latest version is 1.4.3. Definition of AngularJS as put by its official documentation is as follows − AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML”s syntax to express your application”s components clearly and succinctly. Angular”s data binding and dependency injection eliminate much of the code you currently have to write. And it all happens within the browser, making it an ideal partner with any server technology. Features AngularJS is a powerful JavaScript based development framework to create RICH Internet Application(RIA). AngularJS provides developers options to write client side application (using JavaScript) in a clean MVC(Model View Controller) way. Application written in AngularJS is cross-browser compliant. AngularJS automatically handles JavaScript code suitable for each browser. AngularJS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache License version 2.0. Overall, AngularJS is a framework to build large scale and high performance web application while keeping them as easy-to-maintain. Core Features Following are most important core features of AngularJS − Data-binding − It is the automatic synchronization of data between model and view components. Scope − These are objects that refer to the model. They act as a glue between controller and view. Controller − These are JavaScript functions that are bound to a particular scope. Services − AngularJS come with several built-in services for example $https: to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app. Filters − These select a subset of items from an array and returns a new array. Directives − Directives are markers on DOM elements (such as elements, attributes, css, and more). These can be used to create custom HTML tags that serve as new, custom widgets. AngularJS has built-in directives (ngBind, ngModel…) Templates − These are the rendered view with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using “partials”. Routing − It is concept of switching views. Model View Whatever − MVC is a design pattern for dividing an application into different parts (called Model, View and Controller), each with distinct responsibilities. AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular JS team refers it humorously as Model View Whatever. Deep Linking − Deep linking allows you to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state. Dependency Injection − AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test. Concepts Following diagram depicts some important parts of AngularJS which we will discuss in detail in the subsequent chapters. Advantages of AngularJS AngularJS provides capability to create Single Page Application in a very clean and maintainable way. AngularJS provides data binding capability to HTML thus giving user a rich and responsive experience AngularJS code is unit testable. AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. With AngularJS, developer write less code and get more functionality. In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing. On top of everything, AngularJS applications can run on all major browsers and smart phones including Android and iOS based phones/tablets. Disadvantages of AngularJS Though AngularJS comes with lots of plus points but same time we should consider the following points − Not Secure − Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and authorization is must to keep an application secure. Not degradable − If your application user disables JavaScript then user will just see the basic page and nothing more. The AngularJS Components The AngularJS framework can be divided into following three major parts − ng-app − This directive defines and links an AngularJS application to HTML. ng-model − This directive binds the values of AngularJS application data to HTML input controls. ng-bind − This directive binds the AngularJS Application data to HTML tags. AngularJS – Environment Setup In this chapter we will discuss about how to set up AngularJS library to be used in web application development. We will also briefly study the directory structure and its contents. When you open the link https://angularjs.org/, you will see there are two options to download AngularJS library − View on GitHub − Click on this button to go to GitHub and get all of the latest scripts. Download AngularJS 1 − Or click on this button, a screen as below would be seen − This screen gives various options of using Angular JS as follows − Downloading and hosting files locally There are two different options legacy and latest. The names itself are self descriptive. legacy has version less than 1.2.x and latest has 1.5.x version. We can also go with the minified, uncompressed or zipped version. CDN access − You also have access to a CDN. The CDN will give you access around the world to regional data centers that in this case, Google host. This means using CDN moves the responsibility of hosting files from your own servers to a series of external ones. This also offers an advantage that if the visitor to your webpage has already downloaded a copy of AngularJS from the same CDN, it won”t have to be re-downloaded. Try the new angularJS 2 − Click on this button to download Angular JS beta 2 version.This version is very fast, mobile supported and flexible compare to legacy and latest of AngularJS 1 We are using the CDN versions of
AngularJS – Translate Application ”; Previous Next We are providing an example of Translate app. To develop this app, we have used HTML, CSS, translate.js and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;
AngularJS – AJAX
AngularJS – Ajax ”; Previous Next AngularJS provides $http control which works as a service to read data from the server. The server makes a database call to get the desired records. AngularJS needs data in JSON format. Once the data is ready, $http can be used to get the data from server in the following manner − function studentController($scope,$https:) { var url = “data.txt”; $https:.get(url).success( function(response) { $scope.students = response; }); } Here, the file data.txt contains student records. $http service makes an ajax call and sets response to its property students. students model can be used to draw tables in HTML. Examples data.txt [ { “Name” : “Mahesh Parashar”, “RollNo” : 101, “Percentage” : “80%” }, { “Name” : “Dinkar Kad”, “RollNo” : 201, “Percentage” : “70%” }, { “Name” : “Robert”, “RollNo” : 191, “Percentage” : “75%” }, { “Name” : “Julian Joe”, “RollNo” : 111, “Percentage” : “77%” } ] testAngularJS.htm Live Demo <html> <head> <title>Angular JS Includes</title> <style> table, th , td { border: 1px solid grey; border-collapse: collapse; padding: 5px; } table tr:nth-child(odd) { background-color: #f2f2f2; } table tr:nth-child(even) { background-color: #ffffff; } </style> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “” ng-controller = “studentController”> <table> <tr> <th>Name</th> <th>Roll No</th> <th>Percentage</th> </tr> <tr ng-repeat = “student in students”> <td>{{ student.Name }}</td> <td>{{ student.RollNo }}</td> <td>{{ student.Percentage }}</td> </tr> </table> </div> <script> function studentController($scope,$http) { var url = “/data.txt”; $http.get(url).then( function(response) { $scope.students = response.data; }); } </script> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js”> </script> </body> </html> Output To execute this example, you need to deploy testAngularJS.htm and data.txt file to a web server. Open the file testAngularJS.htm using the URL of your server in a web browser and see the result. Print Page Previous Next Advertisements ”;
AngularJS – In-line Application ”; Previous Next We are providing an example of in-line app. To develop this app, we have used HTML, CSS and AngularJS. Using this app, we can change the in-line text as shown below − Source code available at here Print Page Previous Next Advertisements ”;
AngularJS – Dependency Injection ”; Previous Next Dependency Injection is a software design in which components are given their dependencies instead of hard coding them within the component. It relieves a component from locating the dependency and makes dependencies configurable. It also helps in making components reusable, maintainable and testable. AngularJS provides a supreme Dependency Injection mechanism. It provides following core components which can be injected into each other as dependencies. Value Factory Service Provider Constant Value Value is a simple JavaScript object, which is required to pass values to the controller during config phase (config phase is when AngularJS bootstraps itself). //define a module var mainApp = angular.module(“mainApp”, []); //create a value object as “defaultInput” and pass it a data. mainApp.value(“defaultInput”, 5); … //inject the value in the controller using its name “defaultInput” mainApp.controller(”CalcController”, function($scope, CalcService, defaultInput) { $scope.number = defaultInput; $scope.result = CalcService.square($scope.number); $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); Factory Factory is a function which is used to return value. It creates a value on demand whenever a service or a controller requires it. It generally uses a factory function to calculate and return the value. //define a module var mainApp = angular.module(“mainApp”, []); //create a factory “MathService” which provides a method multiply to return multiplication of two numbers mainApp.factory(”MathService”, function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); //inject the factory “MathService” in a service to utilize the multiply method of factory. mainApp.service(”CalcService”, function(MathService) { this.square = function(a) { return MathService.multiply(a,a); } }); … Service Service is a singleton JavaScript object containing a set of functions to perform certain tasks. Service is defined using service() function and it is then injected into the controllers. //define a module var mainApp = angular.module(“mainApp”, []); … //create a service which defines a method square to return square of a number. mainApp.service(”CalcService”, function(MathService) { this.square = function(a) { return MathService.multiply(a,a); } }); //inject the service “CalcService” into the controller mainApp.controller(”CalcController”, function($scope, CalcService, defaultInput) { $scope.number = defaultInput; $scope.result = CalcService.square($scope.number); $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); Provider Provider is used by AngularJS internally to create services, factory, etc. during the config phase. The following script can be used to create MathService that we created earlier. Provider is a special factory method with get() method which is used to return the value/service/factory. //define a module var mainApp = angular.module(“mainApp”, []); … //create a service using provider which defines a method square to return square of a number. mainApp.config(function($provide) { $provide.provider(”MathService”, function() { this.$get = function() { var factory = {}; factory.multiply = function(a, b) { return a * b; } return factory; }; }); }); Constant Constants are used to pass values at the config phase considering the fact that value cannot be used during the config phase. mainApp.constant(“configParam”, “constant value”); Example The following example shows the use of all the above-mentioned directives − testAngularJS.htm Live Demo <html> <head> <title>AngularJS Dependency Injection</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “mainApp” ng-controller = “CalcController”> <p>Enter a number: <input type = “number” ng-model = “number” /></p> <button ng-click = “square()”>X<sup>2</sup></button> <p>Result: {{result}}</p> </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> <script> var mainApp = angular.module(“mainApp”, []); mainApp.config(function($provide) { $provide.provider(”MathService”, function() { this.$get = function() { var factory = {}; factory.multiply = function(a, b) { return a * b; } return factory; }; }); }); mainApp.value(“defaultInput”, 5); mainApp.factory(”MathService”, function() { var factory = {}; factory.multiply = function(a, b) { return a * b; } return factory; }); mainApp.service(”CalcService”, function(MathService) { this.square = function(a) { return MathService.multiply(a,a); } }); mainApp.controller(”CalcController”, function($scope, CalcService, defaultInput) { $scope.number = defaultInput; $scope.result = CalcService.square($scope.number); $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); </script> </body> </html> Output Open testAngularJS.htm in a web browser and see the result. Print Page Previous Next Advertisements ”;
AngularJS – Services
AngularJS – Services ”; Previous Next AngularJS supports the concept of Separation of Concerns using services architecture. Services are JavaScript functions, which are responsible to perform only specific tasks. This makes them individual entities which are maintainable and testable. The controllers and filters can call them on requirement basis. Services are normally injected using the dependency injection mechanism of AngularJS. AngularJS provides many inbuilt services. For example, $http, $route, $window, $location, etc. Each service is responsible for a specific task such as the $http is used to make ajax call to get the server data, the $route is used to define the routing information, and so on. The inbuilt services are always prefixed with $ symbol. There are two ways to create a service − Factory Service Using Factory Method In this method, we first define a factory and then assign method to it. var mainApp = angular.module(“mainApp”, []); mainApp.factory(”MathService”, function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); Using Service Method In this method, we define a service and then assign method to it. We also inject an already available service to it. mainApp.service(”CalcService”, function(MathService) { this.square = function(a) { return MathService.multiply(a,a); } }); Example The following example shows use of all the above mentioned directives − testAngularJS.htm Live Demo <html> <head> <title>Angular JS Services</title> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “mainApp” ng-controller = “CalcController”> <p>Enter a number: <input type = “number” ng-model = “number” /></p> <button ng-click = “square()”>X<sup>2</sup></button> <p>Result: {{result}}</p> </div> <script> var mainApp = angular.module(“mainApp”, []); mainApp.factory(”MathService”, function() { var factory = {}; factory.multiply = function(a, b) { return a * b } return factory; }); mainApp.service(”CalcService”, function(MathService) { this.square = function(a) { return MathService.multiply(a,a); } }); mainApp.controller(”CalcController”, function($scope, CalcService) { $scope.square = function() { $scope.result = CalcService.square($scope.number); } }); </script> </body> </html> Output Open the file testAngularJS.htm in a web browser and see the result. Print Page Previous Next Advertisements ”;
AngularJS – Scopes
AngularJS – Scopes ”; Previous Next Scope is a special JavaScript object that connects controller with views. Scope contains model data. In controllers, model data is accessed via $scope object. <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(“shapeController”, function($scope) { $scope.message = “In shape controller”; $scope.type = “Shape”; }); </script> The following important points are considered in above example − The $scope is passed as first argument to controller during its constructor definition. The $scope.message and $scope.type are the models which are used in the HTML page. We assign values to models that are reflected in the application module, whose controller is shapeController. We can define functions in $scope. Scope Inheritance Scope is controller-specific. If we define nested controllers, then the child controller inherits the scope of its parent controller. <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(“shapeController”, function($scope) { $scope.message = “In shape controller”; $scope.type = “Shape”; }); mainApp.controller(“circleController”, function($scope) { $scope.message = “In circle controller”; }); </script> The following important points are considered in above example − We assign values to the models in shapeController. We override message in child controller named circleController. When message is used within the module of controller named circleController, the overridden message is used. Example The following example shows use of all the above mentioned directives. testAngularJS.htm Live Demo <html> <head> <title>Angular JS Forms</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “mainApp” ng-controller = “shapeController”> <p>{{message}} <br/> {{type}} </p> <div ng-controller = “circleController”> <p>{{message}} <br/> {{type}} </p> </div> <div ng-controller = “squareController”> <p>{{message}} <br/> {{type}} </p> </div> </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(“shapeController”, function($scope) { $scope.message = “In shape controller”; $scope.type = “Shape”; }); mainApp.controller(“circleController”, function($scope) { $scope.message = “In circle controller”; }); mainApp.controller(“squareController”, function($scope) { $scope.message = “In square controller”; $scope.type = “Square”; }); </script> </body> </html> Output Open the file testAngularJS.htm in a web browser and see the result. Print Page Previous Next Advertisements ”;
AngularJS – Cart Application
AngularJS – Cart Application ”; Previous Next We are providing an example of Cart app. To develop this app, we have used HTML, CSS and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;