AngularJS Up and Running Lesson 1: Setup

So i will not bore you by repeating the history of AngularJS and how Google went about creating a MVVC framework to make working and creating Enterprise and Single Page Apps fun, i will jump in to show you how to get this Framework work for you.
Here we will get the Javascript file from the Google CDN (central repository) and start working with AngularJS.
We will create a simple web-page and try to understand the very basic things you need to write to initiate a AngularJS application.
Create a blank html file and write the below code in it, example AngularFirstLesson.html
  1. <!Doctype html>
  2. <html ng-app>
  3. <head>
  4. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  5. </head>
  6. <body>
  7. Enter UserName :<input type="text" ng-model="name" /> {{name}}
  8. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
  9. </body>
  10. </html>
Lets dissect the code to understand whats happening here:
  1. <!Doctype html>
The above code indicates that this is a HTML 5 web page.

  1. <html ng-app>
From Angular Documentation:Use this directive to auto-bootstrap an AngularJS application. ThengApp directive designates the root element of the application and is typically placed near the root element of the page – e.g. on the<body>or<html>tags.
Basically all this says to the application is that get ready this is an AngularJS application.
  1. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>
Here we are loading up the Javascript File from Google CDN.
  1. Enter UserName :<input type="text" ng-model="name" /> {{name}}
This is an interesting piece of code and if you come from Server side programming or have worked only on static html or JSP’s, you will even find it magical when executed.
ng-model from documentation : ”
ngModelis responsible for:
  1. Binding the view into the model, which other directives such asinput,textareaorselectrequire.
  2. Providing validation behavior (i.e. required, number, email, url).
  3. Keeping the state of the control (valid/invalid, dirty/pristine, touched/untouched, validation errors).
  4. Setting related css classes on the element (ng-valid,ng-invalid,ng-dirty,ng-pristine,ng-touched,ng-untouched,ng-empty,ng-not-empty) including animations.
  5. Registering the control with its parent form.
Note:ngModelwill try to bind to the property given by evaluating the expression on the current scope. If the property doesn’t already exist on this scope, it will be created implicitly and added to the scope.”
The crux of this paragraph being that the ngModel directive gives you two way binding using scope, Scope of the current variable defines the binding. Here the data entered in the textfield is assigned to the name variable and the expression .i.e. the value in the {{}} brackets is evaluated and updated in real-time and shown on the view.
Its also very helpful to reduce the code we generally write to access the DOM and setup these variables.No need to have “ids” on all the fields and just setting up properties gets you the values to work with.
Output:
AngularJS Lesson1
AngularJS Lesson1

Thats it for this lesson more coming soon.

Comments