Introduction to AngularJS

AngularJS is a JavaScript framework. It can be added to an HTML page using a <script> tag.

AngularJS throughdirectivesextends HTML attributes and usesExpressionsto bind data to HTML.

AngularJS is a JavaScript framework

AngularJS is a JavaScript framework written in JavaScript.

AngularJS is distributed in the form of JavaScript files and can be added to a web page using the script tag:

<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>

AngularJS extends HTML

AngularJS uses ng-directiveIt extends HTML.

ng-app The instruction defines an AngularJS application.

ng-model The instruction binds the value of HTML controls (input, select, textarea) to application data.

ng-bind The instruction binds application data to the HTML view.

AngularJS Examples

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>Name: <input type="text" ng-model="name"></p>
  <p ng-bind="name"></p>
</div>
</body>
</html>

Try It Yourself

Example explanation:

AngularJS will automatically start when the web page is loaded.

ng-app tells AngularJS that the <div> element is an AngularJS Applicationof the 'owner'.

ng-model The instruction binds the value of the input field to the application variable. name.

ng-bind Directives bind the content of the <p> element to the application variable name.

AngularJS Directives

As you can see, AngularJS directives are with ng prefix HTML attributes.

ng-init Directives initialize AngularJS application variables.

AngularJS Examples

<div ng-app="" ng-init="firstName='Bill'">
<p>Name is <span ng-bind="firstName"></span></p>
</div>

Try It Yourself

or use valid HTML:

AngularJS Examples

<div data-ng-app="" data-ng-init="firstName='Bill'">
<p>Name is <span data-ng-bind="firstName"></span></p>
</div>

Try It Yourself

If you want the HTML page to be valid, you can use data-ng- instead of ng-.

You will learn more about directives later in this tutorial.

AngularJS Expressions

AngularJS expressions are written inside double curly braces:{{ expression }}.

AngularJS will "output" data at the location of the expression:

AngularJS Examples

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>

Try It Yourself

AngularJS expressions bind AngularJS data to HTML, similar to ng-bind In the same way as directives.

AngularJS Examples

<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
  <p>Name: <input type="text" ng-model="name"></p>
  <p>{{name}}</p>
</div>
</body>
</html>

Try It Yourself

You will learn more about expressions later in this tutorial.

AngularJS Applications

AngularJS ModuleDefine AngularJS application.

AngularJS ControllerControl AngularJS application.

ng-app Directive defines an application,ng-controller Directive defines a controller.

AngularJS Examples

<div ng-app="myApp" ng-controller="myCtrl">
Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.firstName= "Bill";
  $scope.lastName= "Gates";
});
</script>

Try It Yourself

AngularJS Modules define the application:

AngularJS Modules

var app = angular.module('myApp', []);

AngularJS Controllers control the application:

AngularJS Controllers

app.controller('myCtrl', function($scope) {
  $scope.firstName= "Bill";
  $scope.lastName= "Gates";
});

You will learn more about modules and controllers later in this tutorial.