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.
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 applications are a combination of HTML and JavaScript, so the first thing you need an HTML page:
<html> <head> <title>My first AngularJS code</title> </Script> </head> <body> </body> </html>
Now, you need to include the AngularJS file in the HTML page so you can use use all attributes of AngularJS:
<html> <head> <title>My first AngularJS code</title> <Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"> </Script> </head> <body> </body> </html>
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.
<html ng-app> <head> <title>My first AngularJS code</title> <Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"> </Script> </head> <body> </body> </html>
<html ng-app>
<head>
<title>My first AngularJS code</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
</head>
<body>
</body>
</html>
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.
<html ng-app>
<head>
<title>My first AngularJS code</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
</head>
<body>
<p>Input string in below box:</p>
<input ng-model="myName" type="text" placeholder="Your name">
<h1>Hey {{ myName }}</h1>
</body>
</html>