AngularJS is a JavaScript framework. It can be added to an HTML page with a <script> tag.
AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.
AngularJS extends HTML with ng-directives.
The ng-app directive defines an AngularJS application.
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
The ng-bind directive binds application data to the HTML view.
Introduction
<html>
<head>
<title>Angular JS Example</title>
<script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
</body>
</html>
AngularJS expressions can be written inside double braces: {{ expression }}.
AngularJS expressions can also be written inside a directive: ng-bind="expression".
AngularJS will resolve the expression, and return the result exactly where the expression is written.
AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.
Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
If you remove the ng-app directive, HTML will display the expression as it is, without solving it:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div>
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
You can write expressions wherever you like, AngularJS will simply resolve the expression and return the result.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-init="myCol='lightblue'">
<input style="background-color:{{myCol}}" ng-model="myCol">
</div>
</body>
</html>
AngularJS objects are like JavaScript objects:
<html>
<script src="angular.min.js"></script>
<body>
<div ng-app="" ng-init="person={firstName:Ravi',lastName:'Chythanya'}">
<p>The name is: {{person.firstName +' ' +person.lastName }}</p>
</div>
</body>
</html>
AngularJS arrays are like JavaScript arrays:
<html>
<script src="angular.min.js"></script>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The name is: {{ points[2] }}</p>
</div>
</body>
</html>
An AngularJS module defines an application.
The module is a container for the different parts of an application.
The module is a container for the application controllers.
Controllers always belong to a module.
A module is created by using the AngularJS function angular.module as follows:
<div ng-app="myApp">...</div>
<script>
var app = angular.module("myApp", []);
</script>
You can use the module to add your own directives to your applications:
<script>
var app = angular.module('myApp', []);
app.directive("myDirective", function() {
return {
template : "I was made in a directive constructor!"
};
});
</script>
</body>
</html>
AngularJS lets you extend HTML with new attributes called Directives.
AngularJS has a set of built-in directives which offers functionality to your applications.
AngularJS also lets you define your own directives.
AngularJS directives are extended HTML attributes with the prefix ng-.
The ng-repeat directive repeats an HTML element:
<html>
<script src="angular.min.js"></script>
<body>
<div ng-app="" ng-init="subjects=['WT','TOC','OS']">
<ul>
<li ng-repeat="x in subjects">{{ x }}</li>
</ul>
</div>
</body>
</html>
To invoke the new directive, make an HTML element with the same tag name as the new directive.
When naming a directive, you must use a camel case name, myDirective, but when invoking it, you must use - separated name, my-directive:
AngularJS arrays are like JavaScript arrays:
<body ng-app="myApp">
<my-directive></my-directive>
<!--div my-directive></div-->
<script>
var app = angular.module("myApp", []);
app.directive("myDirective", function() {
return {
template : "<h1>Made by a directive!</h1>"
};
});
</script>
</body>
AngularJS controllers control the data of AngularJS applications.
AngularJS controllers are regular JavaScript Objects.
The ng-controller directive defines the application controller.
A controller is a JavaScript Object, created by a standard JavaScript object constructor.
<html>
<script src="angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
First 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 = "Ravi";
$scope.lastName = "Chythanya";
});
</script>
</body>
</html>
A controller can also have methods (variables as functions):
<html>
<script src="angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "Ravi";
$scope.lastName = "Chythanya";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
AngularJS has its own HTML events directives.
Increase the count variable when the mouse moves over the H1 element:
<div ng-app="myApp" ng-controller="myCtrl">
<h1 ng-mousemove="count = count + 1">Mouse over me!</h1>
<h2>{{ count }}</h2>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
</script>
<html>
<script src="angular.min.js"></script>
<body ng-app="">
<input ng-paste="count = count + 1" ng-init="count=0" value="Paste text here!" />
<p>Text has been pasted {{count}} times.</p>
<p>This example will increase the value of the variable "count" every time you paste text into the input field.</p>
</body>
</html>
<button ng-click="myFunc()">Click Me!</button>
<div ng-show="showMe">
<h1>Menu:</h1>
<div>Pizza</div>
<div>Pasta</div>
<div>Pesce</div>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.showMe = false;
$scope.myFunc = function() { $scope.showMe = !$scope.showMe; }
});