AngularJS – Switch Menu

AngularJS – Switch Menu ”; Previous Next We are providing an example of Switch menu. To develop this app, we have used HTML, CSS and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;

AngularJS – Directives

AngularJS – Directives ”; Previous Next AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix. Let us discuss the following directives − ng-app − This directive starts an AngularJS Application. ng-init − This directive initializes application data. ng-model − This directive defines the model that is variable to be used in AngularJS. ng-repeat − This directive repeats HTML elements for each item in a collection. ng-app directive The ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application when the web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application. In the following example, we define a default AngularJS application using ng-app attribute of a <div> element. <div ng-app = “”> … </div> ng-init directive The ng-init directive initializes an AngularJS Application data. It is used to assign values to the variables. In the following example, we initialize an array of countries. We use JSON syntax to define the array of countries. <div ng-app = “” ng-init = “countries = [{locale:”en-US”,name:”United States”}, {locale:”en-GB”,name:”United Kingdom”}, {locale:”en-FR”,name:”France”}]”> … </div> ng-model directive The ng-model directive defines the model/variable to be used in AngularJS Application. In the following example, we define a model named name. <div ng-app = “”> … <p>Enter your Name: <input type = “text” ng-model = “name”></p> </div> ng-repeat directive The ng-repeat directive repeats HTML elements for each item in a collection. In the following example, we iterate over the array of countries. <div ng-app = “”> … <p>List of Countries with locale:</p> <ol> <li ng-repeat = “country in countries”> {{ ”Country: ” + country.name + ”, Locale: ” + country.locale }} </li> </ol> </div> Example The following example shows the use of all the above-mentioned directives. testAngularJS.htm Live Demo <html> <head> <title>AngularJS Directives</title> </head> <body> <h1>Sample Application</h1> <div ng-app = “” ng-init = “countries = [{locale:”en-US”,name:”United States”}, {locale:”en-GB”,name:”United Kingdom”}, {locale:”en-FR”,name:”France”}]”> <p>Enter your Name: <input type = “text” ng-model = “name”></p> <p>Hello <span ng-bind = “name”></span>!</p> <p>List of Countries with locale:</p> <ol> <li ng-repeat = “country in countries”> {{ ”Country: ” + country.name + ”, Locale: ” + country.locale }} </li> </ol> </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> </body> </html> Output Open the file testAngularJS.htm in a web browser. Enter your name and see the result. Print Page Previous Next Advertisements ”;

AngularJS – Internationalization

AngularJS – Internationalization ”; Previous Next AngularJS supports inbuilt internationalization for three types of filters : Currency, Date, and Numbers. We only need to incorporate corresponding java script according to locale of the country. By default, it considers the locale of the browser. For example, for Danish locale, use the following script − <script src = “https://code.angularjs.org/1.2.5/i18n/angular-locale_da-dk.js”> </script> Example Using Danish Locale testAngularJS.htm Live Demo <html> <head> <title>Angular JS Forms</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “mainApp” ng-controller = “StudentController”> {{fees | currency }} <br/><br/> {{admissiondate | date }} <br/><br/> {{rollno | number }} </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> <script src = “https://code.angularjs.org/1.3.14/i18n/angular-locale_da-dk.js”> </script> <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(”StudentController”, function($scope) { $scope.fees = 100; $scope.admissiondate = new Date(); $scope.rollno = 123.45; }); </script> </body> </html> Output Open the file testAngularJS.htm in a web browser and see the result. Example Using Browser Locale testAngularJS.htm Live Demo <html> <head> <title>Angular JS Forms</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “mainApp” ng-controller = “StudentController”> {{fees | currency }} <br/><br/> {{admissiondate | date }} <br/><br/> {{rollno | number }} </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> <!– <script src = “https://code.angularjs.org/1.3.14/i18n/angular-locale_da-dk.js”> </script> –> <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(”StudentController”, function($scope) { $scope.fees = 100; $scope.admissiondate = new Date(); $scope.rollno = 123.45; }); </script> </body> </html> Output Open the file testAngularJS.htm in a web browser and see the result. Print Page Previous Next Advertisements ”;

AngularJS – Login Application

AngularJS – Login Application ”; Previous Next We are providing an example of Login app. To develop this app, we have used HTML, CSS and AngularJS. Source code available at here Print Page Previous Next Advertisements ”;

AngularJS – HTML DOM

AngularJS – HTML DOM ”; Previous Next The following directives are used to bind application data to the attributes of HTML DOM elements − Sr.No. Name & Description 1 ng-disabled disables a given control. 2 ng-show shows a given control. 3 ng-hide hides a given control. 4 ng-click represents a AngularJS click event. ng-disabled Directive Add ng-disabled attribute to an HTML button and pass it a model. Bind the model to a checkbox and see the variation. <input type = “checkbox” ng-model = “enableDisableButton”>Disable Button <button ng-disabled = “enableDisableButton”>Click Me!</button> ng-show Directive Add ng-show attribute to an HTML button and pass it a model. Bind the model to a checkbox and see the variation. <input type = “checkbox” ng-model = “showHide1”>Show Button <button ng-show = “showHide1”>Click Me!</button> ng-hide Directive Add ng-hide attribute to an HTML button and pass it a model. Bind the model to a checkbox and see the variation. <input type = “checkbox” ng-model = “showHide2”>Hide Button <button ng-hide = “showHide2”>Click Me!</button> ng-click Directive Add ng-click attribute to an HTML button and update a model. Bind the model to HTML and see the variation. <p>Total click: {{ clickCounter }}</p> <button ng-click = “clickCounter = clickCounter + 1”>Click Me!</button> Example The following example shows use of all the above mentioned directives. testAngularJS.htm Live Demo <html> <head> <title>AngularJS HTML DOM</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = “”> <table border = “0”> <tr> <td><input type = “checkbox” ng-model = “enableDisableButton”>Disable Button</td> <td><button ng-disabled = “enableDisableButton”>Click Me!</button></td> </tr> <tr> <td><input type = “checkbox” ng-model = “showHide1”>Show Button</td> <td><button ng-show = “showHide1”>Click Me!</button></td> </tr> <tr> <td><input type = “checkbox” ng-model = “showHide2”>Hide Button</td> <td><button ng-hide = “showHide2”>Click Me!</button></td> </tr> <tr> <td><p>Total click: {{ clickCounter }}</p></td> <td><button ng-click = “clickCounter = clickCounter + 1”>Click Me!</button></td> </tr> </table> </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> </body> </html> Output Open the file testAngularJS.htm in a web browser and see the result. Print Page Previous Next Advertisements ”;

AngularJS – Filters

AngularJS – Filters ”; Previous Next Filters are used to modify the data. They can be clubbed in expression or directives using pipe (|) character. The following list shows the commonly used filters. Sr.No. Name & Description 1 uppercase converts a text to upper case text. 2 lowercase converts a text to lower case text. 3 currency formats text in a currency format. 4 filter filter the array to a subset of it based on provided criteria. 5 orderby orders the array based on provided criteria. Uppercase Filter Add uppercase filter to an expression using pipe character. Here we”ve added uppercase filter to print student name in all capital letters. Enter first name:<input type = “text” ng-model = “student.firstName”> Enter last name: <input type = “text” ng-model = “student.lastName”> Name in Upper Case: {{student.fullName() | uppercase}} Lowercase Filter Add lowercase filter to an expression using pipe character. Here we”ve added lowercase filter to print student name in all lowercase letters. Enter first name:<input type = “text” ng-model = “student.firstName”> Enter last name: <input type = “text” ng-model = “student.lastName”> Name in Lower Case: {{student.fullName() | lowercase}} Currency Filter Add currency filter to an expression returning number using pipe character. Here we”ve added currency filter to print fees using currency format. Enter fees: <input type = “text” ng-model = “student.fees”> fees: {{student.fees | currency}} Filter To display only required subjects, we use subjectName as filter. Enter subject: <input type = “text” ng-model = “subjectName”> Subject: <ul> <li ng-repeat = “subject in student.subjects | filter: subjectName”> {{ subject.name + ”, marks:” + subject.marks }} </li> </ul> OrderBy Filter To order subjects by marks, we use orderBy marks. Subject: <ul> <li ng-repeat = “subject in student.subjects | orderBy:”marks””> {{ subject.name + ”, marks:” + subject.marks }} </li> </ul> Example The following example shows use of all the above mentioned filters. testAngularJS.htm Live Demo <html> <head> <title>Angular JS Filters</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 = “studentController”> <table border = “0”> <tr> <td>Enter first name:</td> <td><input type = “text” ng-model = “student.firstName”></td> </tr> <tr> <td>Enter last name: </td> <td><input type = “text” ng-model = “student.lastName”></td> </tr> <tr> <td>Enter fees: </td> <td><input type = “text” ng-model = “student.fees”></td> </tr> <tr> <td>Enter subject: </td> <td><input type = “text” ng-model = “subjectName”></td> </tr> </table> <br/> <table border = “0”> <tr> <td>Name in Upper Case: </td><td>{{student.fullName() | uppercase}}</td> </tr> <tr> <td>Name in Lower Case: </td><td>{{student.fullName() | lowercase}}</td> </tr> <tr> <td>fees: </td><td>{{student.fees | currency}} </td> </tr> <tr> <td>Subject:</td> <td> <ul> <li ng-repeat = “subject in student.subjects | filter: subjectName |orderBy:”marks””> {{ subject.name + ”, marks:” + subject.marks }} </li> </ul> </td> </tr> </table> </div> <script> var mainApp = angular.module(“mainApp”, []); mainApp.controller(”studentController”, function($scope) { $scope.student = { firstName: “Mahesh”, lastName: “Parashar”, fees:500, subjects:[ {name:”Physics”,marks:70}, {name:”Chemistry”,marks:80}, {name:”Math”,marks:65} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + ” ” + studentObject.lastName; } }; }); </script> </body> </html> Output Open the file testAngularJS.htm in a web browser. See the result. Print Page Previous Next Advertisements ”;

AngularJS – MVC Architecture

AngularJS – MVC Architecture ”; Previous Next Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View Controller pattern is made up of the following three parts − Model − It is the lowest level of the pattern responsible for maintaining data. View − It is responsible for displaying all or a portion of the data to the user. Controller − It is a software Code that controls the interactions between the Model and View. MVC is popular because it isolates the application logic from the user interface layer and supports separation of concerns. The controller receives all requests for the application and then works with the model to prepare any data needed by the view. The view then uses the data prepared by the controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows. The Model The model is responsible for managing application data. It responds to the request from view and to the instructions from controller to update itself. The View A presentation of data in a particular format, triggered by the controller”s decision to present the data. They are script-based template systems such as JSP, ASP, PHP and very easy to integrate with AJAX technology. The Controller The controller responds to user input and performs interactions on the data model objects. The controller receives input, validates it, and then performs business operations that modify the state of the data model. AngularJS is a MVC based framework. In the coming chapters, we will see how AngularJS uses MVC methodology. Print Page Previous Next Advertisements ”;

AngularJS – Environment Setup

AngularJS – Environment Setup ”; Previous Next This chapter describes how to set up AngularJS library to be used in web application development. It also briefly describes 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 − By clicking on this button, you are diverted to GitHub and get all the latest scripts. Download AngularJS 1 − By clicking on this button, a screen you get to see a dialog box shown as − 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 themselves are self-descriptive. The Legacy has version less than 1.2.x and the Latest come with version 1.3.x. We can also go with the minimized, uncompressed, or zipped version. CDN access − You also have access to a CDN. The CDN gives you access to regional data centers. In this case, the Google host. The CDN transfers the responsibility of hosting files from your own servers to a series of external ones. It also offers an advantage that if the visitor of your web page has already downloaded a copy of AngularJS from the same CDN, there is no need to re-download it. We are using the CDN versions of the library throughout this tutorial. Example Now let us write a simple example using AngularJS library. Let us create an HTML file myfirstexample.html shown as below − Live Demo <!doctype html> <html> <head> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js”></script> </head> <body ng-app = “myapp”> <div ng-controller = “HelloController” > <h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2> </div> <script> angular.module(“myapp”, []) .controller(“HelloController”, function($scope) { $scope.helloTo = {}; $scope.helloTo.title = “AngularJS”; }); </script> </body> </html> Let us go through the above code in detail − Include AngularJS We include the AngularJS JavaScript file in the HTML page so that we can use it − <head> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js”> </script> </head> You can check the latest version of AngularJS on its official website. Point to AngularJS app Next, it is required to tell which part of HTML contains the AngularJS app. You can do this by adding the ng-app attribute to the root HTML element of the AngularJS app. You can either add it to the html element or the body element as shown below − <body ng-app = “myapp”> </body> View The view is this part − <div ng-controller = “HelloController” > <h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2> </div> ng-controller tells AngularJS which controller to use with this view. helloTo.title tells AngularJS to write the model value named helloTo.title in HTML at this location. Controller The controller part is − <script> angular.module(“myapp”, []) .controller(“HelloController”, function($scope) { $scope.helloTo = {}; $scope.helloTo.title = “AngularJS”; }); </script> This code registers a controller function named HelloController in the angular module named myapp. We will study more about modules and controllers in their respective chapters. The controller function is registered in angular via the angular.module(…).controller(…) function call. The $scope parameter model is passed to the controller function. The controller function adds a helloTo JavaScript object, and in that object it adds a title field. Execution Save the above code as myfirstexample.html and open it in any browser. You get to see the following output − Welcome AngularJS to the world of Tutorialspoint! What happens when the page is loaded in the browser ? Let us see − HTML document is loaded into the browser, and evaluated by the browser. AngularJS JavaScript file is loaded, the angular global object is created. The JavaScript which registers controller functions is executed. Next, AngularJS scans through the HTML to search for AngularJS apps as well as views. Once the view is located, it connects that view to the corresponding controller function. Next, AngularJS executes the controller functions. It then renders the views with data from the model populated by the controller. The page is now ready. Print Page Previous Next Advertisements ”;

AngularJS – First Application

AngularJS – First Application ”; Previous Next Before creating actual Hello World ! application using AngularJS, let us see the parts of a AngularJS application. An AngularJS application consists of following three important 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. Creating AngularJS Application Step 1: Load framework Being a pure JavaScript framework, it can be added using <Script> tag. <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> Step 2: Define AngularJS application using ng-app directive <div ng-app = “”> … </div> Step 3: Define a model name using ng-model directive <p>Enter your Name: <input type = “text” ng-model = “name”></p> Step 4: Bind the value of above model defined using ng-bind directive <p>Hello <span ng-bind = “name”></span>!</p> Executing AngularJS Application Use the above-mentioned three steps in an HTML page. testAngularJS.htm Live Demo <html> <head> <title>AngularJS First Application</title> </head> <body> <h1>Sample Application</h1> <div ng-app = “”> <p>Enter your Name: <input type = “text” ng-model = “name”></p> <p>Hello <span ng-bind = “name”></span>!</p> </div> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”> </script> </body> </html> Output Open the file testAngularJS.htm in a web browser. Enter your name and see the result. How AngularJS Integrates with HTML The ng-app directive indicates the start of AngularJS application. The ng-model directive creates a model variable named name, which can be used with the HTML page and within the div having ng-app directive. The ng-bind then uses the name model to be displayed in the HTML <span> tag whenever user enters input in the text box. Closing</div> tag indicates the end of AngularJS application. Print Page Previous Next Advertisements ”;

AngularJS – Modules

AngularJS – Modules ”; Previous Next AngularJS supports modular approach. Modules are used to separate logic such as services, controllers, application etc. from the code and maintain the code clean. We define modules in separate js files and name them as per the module.js file. In the following example, we are going to create two modules − Application Module − used to initialize an application with controller(s). Controller Module − used to define the controller. Application Module Here is a file named mainApp.js that contains the following code − var mainApp = angular.module(“mainApp”, []); Here, we declare an application mainApp module using angular.module function and pass an empty array to it. This array generally contains dependent modules. Controller Module studentController.js mainApp.controller(“studentController”, function($scope) { $scope.student = { firstName: “Mahesh”, lastName: “Parashar”, fees:500, subjects:[ {name:”Physics”,marks:70}, {name:”Chemistry”,marks:80}, {name:”Math”,marks:65}, {name:”English”,marks:75}, {name:”Hindi”,marks:67} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + ” ” + studentObject.lastName; } }; }); Here, we declare a controller studentController module using mainApp.controller function. Use Modules <div ng-app = “mainApp” ng-controller = “studentController”> … <script src = “mainApp.js”></script> <script src = “studentController.js”></script> </div> Here, we use application module using ng-app directive, and controller using ngcontroller directive. We import the mainApp.js and studentController.js in the main HTML page. Example The following example shows use of all the above mentioned modules. testAngularJS.htm Live Demo <html> <head> <title>Angular JS Modules</title> <script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”></script> <script src = “/angularjs/src/module/mainApp.js”></script> <script src = “/angularjs/src/module/studentController.js”></script> <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 = “mainApp” ng-controller = “studentController”> <table border = “0”> <tr> <td>Enter first name:</td> <td><input type = “text” ng-model = “student.firstName”></td> </tr> <tr> <td>Enter last name: </td> <td><input type = “text” ng-model = “student.lastName”></td> </tr> <tr> <td>Name: </td> <td>{{student.fullName()}}</td> </tr> <tr> <td>Subject:</td> <td> <table> <tr> <th>Name</th> <th>Marks</th> </tr> <tr ng-repeat = “subject in student.subjects”> <td>{{ subject.name }}</td> <td>{{ subject.marks }}</td> </tr> </table> </td> </tr> </table> </div> </body> </html> mainApp.js var mainApp = angular.module(“mainApp”, []); studentController.js mainApp.controller(“studentController”, function($scope) { $scope.student = { firstName: “Mahesh”, lastName: “Parashar”, fees:500, subjects:[ {name:”Physics”,marks:70}, {name:”Chemistry”,marks:80}, {name:”Math”,marks:65}, {name:”English”,marks:75}, {name:”Hindi”,marks:67} ], fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + ” ” + studentObject.lastName; } }; }); Output Open the file textAngularJS.htm in a web browser. See the result. Print Page Previous Next Advertisements ”;