Angularjs Examples

AJS Examples


AngularJS Introduction

AngularJS is one of the best platform that enable developers to build structured web applications that are robust and easily to understood.

AngularJS is a powerful Javascript framework to provide complete client-side solution, specially when you building single-page web applications. AngularJS introduce new attributes to extend the HTML controls features.

This tutorial will explain you how to use AngularJS, its advantages and start writing AngularJS code from scratch.

It's data binding and dependency injection reduce extra overhead of code that otherwise have to write. AngularJS is open source and absolutely free to use only you need to include 'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js' file.

Why AngularJS?

AngularJS is based on MVC framework that defines how to oganize your web application. Your application is defined with modules that can depend from one to the others. It extend HTML control features by attaching directives to web pages with new attributes and expressions in order to define very powerful templates directly in your HTML. The controllers encapsulates the behavior of application. AngularJS helps to structure and test your Javascript code very easily.

AngularJS Advantages

  • Reusable template creation is easy.
  • Two way data binding to any element is easy, where changing data will automatically change element and changing element will change the data.
  • You can directly call the code-behind code in your html.
  • Forms validation before submitting it without writing a single line of code.
  • Allows structure code writing, where code is divided into modal, view and controler which is easy to maintain.
  • AngularJS allows you to write basic flow end-to-end testing, unit-testing, ui mocks.

Developing an AngularJS Application From Scratch

AngularJS applications are a combination of HTML and JavaScript, so the first thing you need an HTML page:

Now, you need to include the AngularJS file in the HTML page so you can use use all attributes of AngularJS:

Now, you need to specify AngularJS section in your HTML page. To do this you need to add ng-app attribute to the root element of the section where you want to add AngularJS. Typically, root element can be HTML, BODY or DIV element of page. Below example shows ng-app attribute added in HTML tag.

 

Now, you have to add a HTML input text element that contains ng-model attribute. In below example contains h1 HTML element with AngularJS expression {{ myName }} which tells AngularJS to insert the model value myName into the HTML at that location. This will bind both elements, if user insert any value in text box it will automatic update the value in HTML h1 element.

AngularJS Example: