This article provides a simple hands-on application for AngularJS. First let us get a small explanation of AngularJS before proceeding and implementing it.
What is AngularJS
AngularJS is an open-source web application framework maintained by Google that was released in 2009 and written in JavaScript. It extends HTML attributes with directives and binds data to HTML with expressions.
The link to AngularJS distribution file is:
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
Why to use AngularJS
The following are some of the reasons to use AngularJS:
- Angular acts as the mediator, developers also won't feel tempted to write shortcuts between components that break abstractions just to make them fit easier.
- Directives come in the form of custom HTML elements.
- Filters are so resourceful that it is possible to create a sortable HTML table using only filters without writing any JavaScript.
- Write less code.
Let's proceed and create a HTML home page as in the following:
- <!DOCTYPE html>
-
-
- <html ng-app="MyApp">
-
- <head>
-
- <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
- <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
-
-
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
- <script src="script.js"></script>
- </head>
-
-
- <body ng-controller="mainController">
-
- <nav class="navbar navbar-default">
- <div class="container">
- <div class="navbar-header">
- <a class="navbar-brand" href="/">Angular Routing Example</a>
- </div>
-
- <ul class="nav navbar-nav navbar-right">
- <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
- <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
- <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
- </ul>
- </div>
- </nav>
-
- <div id="main">
-
- <div ng-view></div>
-
- </div>
- </body>
-
- </html>
In the preceding Index page, we created static HTML where we are referring to JavaScript libraries and finally a main div for injecting the HTML of the other views.
The following is the code of the JavaScript file where we will do the routing of the application to redirect to the corresponding pages:
- // create the module and name it MyApp
- var MyApp = angular.module(' MyApp ', ['ngRoute']);
-
- // configure our routes
- MyApp.config(function($routeProvider) {
- $routeProvider
-
- // route for the home page
- .when('/', {
- templateUrl : 'pages/home.html',
- controller : 'mainController'
- })
-
- // route for the about page
- .when('/about', {
- templateUrl : 'pages/about.html',
- controller : 'aboutController'
- })
-
- // route for the contact page
- .when('/contact', {
- templateUrl : 'pages/contact.html',
- controller : 'contactController'
- });
- });
The following is the code where we can inject the code for the controller:
- // create the controller and inject Angular's $scope
- MyApp.controller('mainController', function($scope) {
- // create a message to display in our view
- $scope.message = 'Hello Everyone ! I am in Home page !';
- });
-
- MyApp.controller('aboutController', function($scope) {
- $scope.message = 'Hello Everyone ! I am in About page !';
- });
-
- MyApp.controller('contactController', function($scope) {
- $scope.message = 'Hello Everyone ! I am in Contact page !';
- });
I hope you liked the article. Please write your comments and views on this.