Tuesday, January 13, 2015

Single page Web App using Angular JS

index.html
This is the simple part. We’re using Bootstrap and Font Awesome. Open up your index.html file and we’ll add a simple layout with a navigation bar.

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Single page web app using Angularjs</title>


<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular-route.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
  <script src="script.js"></script>


</head>
<body ng-app="shaharma">

  <div>
      <div>
          <nav class="navbar navbar-inverse" role="navigation" style="padding-left:130px;">
             <ul class="nav navbar-nav">
                <li class="active"><a href="#/">Home<span class="sr-only">(current)</span></a></li>
                <li><a href="#/about">About us</a></li>
                <li><a href="#/about">Read tutorial</a></li>
              </ul>
          </nav>
      </div>

      <br/>

      <div ng-view class="jumbotron"></div> <!-- div where the dynamic page view will be loaded -->

  </div>
</body>

</html>

Injecting Pages into the Main Layout

ng-view is an Angular directive that will include the template of the current route (/home, /about, or /contact) in the main layout file. In plain words, it takes the file we want based on the route and injects it into our main layout (index.html).
We will add the ng-view code to our site in the div#maito tell Angular where to place our rendered pages.

Configure Routes and Views

Since we are making a single page application and we don’t want any page refreshes, we’ll use Angular’s routing capabilities.
Let’s look in our Angular file and add to our application. We will be using $routeProvider in Angular to handle our routing. This way, Angular will handle all of the magic required to go get a new file and inject it into our layout.
For linking to pages, we’ll use the #. We don’t want the browser to think we are actually travelling to about.html or home.html
First, we have to create our module and controller in javascript. We will do that now in script.js.

script.js.
var app = angular.module('shaharma',['ngRoute']);

app.config(function($routeProvider){

      $routeProvider
          .when('/',{
                templateUrl: 'home.html'
          })
          .when('/about',{
                templateUrl: 'about.html',
                controller:'yubrajcontroller'
          });


});

app.controller('yubrajcontroller', ['$scope', '$http', function($scope, $http){   ---[A]
   $scope.message = 'Welcome to Inspire';
  $http.get("http://www.w3schools.com/website/Customers_JSON.php")
.success(function(response) {$scope.names = response;});
}]);


--[A]- description
here we are fetching the json data from the http server which will be shown in the about.html file



about.html.

<div style="padding-left:130px;padding-right:200px;">

<h1>About us page2. {{message}} </h1>

<p><input type="text" ng-model="searchresult"></p>
<table class="table">
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
<tr ng-repeat="x in names | filter:searchresult">

<!-- here names comes from the yubrajcontroller which is returning the json data to the about us page -->

<td>{{ x.Name | uppercase }}</td>
<td>{{ x.City | uppercase }}</td>
<td>{{ x.Country | uppercase }}</td>
</tr>

  </table>

 </div>


home.html

 <div style="padding-left:130px;padding-right:200px;">
   <h1>Hello, world!</h1>
  <p>This is simple angular ng-view and ng-route tutorial to demonstrate single page web app development.</p>
  <p>
    This is executed because of this code.
    <pre>
      <code>
        .when('/',{
              templateURl: 'home.html'
        })
      </code>
    </pre>
  </p>
  <p><a class="btn btn-primary btn-lg" href="#" role="button">Read tutorial</a></p>
</div>

--Enjoy

Getting Started with Angular JS

official site of angular Js provide us two options for the use of Angular JS library i.e. Github and a local file for download as seen in the page https://angularjs.org/




  • AngularJS DownloadThis screen offers various options for selecting 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.

Advantages/Disadvantages of Angular JS

Advantages of AngularJS

The advantages of AngularJS are:
  • It provides the capability to create Single Page Application in a very clean and maintainable way.
  • It provides data binding capability to HTML. Thus, it gives 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, the developers can achieve more functionality with short code.
  • In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing.
On the 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 a lot of merits, here are some points of concern:
1. 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.
2. Not degradable: If the user of your application disables JavaScript, then nothing would be visible, except the basic page.

AngularJS Directives

The AngularJS framework can be divided into three major parts:
1. ng-app : This directive defines and links an AngularJS application to HTML.
2. ng-model : This directive binds the values of AngularJS application data to HTML input controls.
3. ng-bind : This directive binds the AngularJS application data to HTML tags.

Features of Angular JS

Core Features of Angular JS

The core features of AngularJS are as follows:
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 bound to a particular scope.
Services: AngularJS comes with several built-in services such as $http 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 such as ngBind, ngModel, etc.
Templates:These are the rendered view with information from the controller and model. These can be a single file (such as index.html) or multiple views in one page using partials.
Routing: It is concept of switching views.
Model View Whatever: MVW 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 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 to create, understand, and test the applications easily.

What is Anguar JS - First Step

AngularJS is quite new. Version 1.0 was released in 2012. Miško Hevery, a Google employee, started to work on AngularJS in 2009. The idea turned out very good, and the project is now officially backed by a Google development team.
AngularJS is a JavaScript framework. It is a library written in JavaScript.
AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag:

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>