Angular js Mod3
Angular js Mod3
Introduction to AngularJS
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
What is an AngularJS??
• MVC JavaScript Framework by Google
• Rich Web Application Development
• Extends HTML DOM with additional attributes
• Open source
• Static HTML to Dynamic HTML
• Single Page Application
• Licensed under the Apache license version 2.0
• Sites- Nasa.gov, stackoverflow.com, Netflix, Youtube
AngularJS MVC Architecture
Definition of AngularJS
• Cross-browser compliant
• Scope − objects.
• Controller − function.
• ng-bind − This directive binds the AngularJS Application data to HTML tags.
First App - steps
Step 1: Load framework
Step 2: Define AngularJS application using ng-app directive
Step 3: Define a model name using ng-model directive
Step 4: Bind the value of above model defined using ng-bind directive
First App - steps
Step 1: Load framework
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
Step 4: Bind the value of above model defined using ng-bind directive
First App - steps
Step 1: Load framework
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
Step 4: Bind the value of above model defined using ng-bind directive
First App - steps
Step 1: Load framework
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
Step 2: Define AngularJS application using ng-app directive
<div ng-app = ""> ... </div>
Step 3: Define a model name using ng-model directive
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
Step 4: Bind the value of above model defined using ng-bind directive
<p>Hello <span ng-bind = "name"></span>!</p>
First App – test.html
<html> <head>
<title>AngularJS First Application</title> </head>
<body>
<h1>Sample Application</h1>
<div ng-app = "">
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
<p>Hello <span ng-bind = "name"></span>!</p>
</div>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</body>
</html>
• Open test.html in browser
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS modules
• Defines an application.
• Container for controller, services, filters, directives etc.
• Main() method
• Separates logic from code.
• Separate .js files
AngularJS modules – How to create??
• The angular object's module() method is used to create a
module.
<div ng-app="myApp">...</div>
<script>
var app = angular.module("myApp", []);
</script>
Module Array of
Name dependent
modules
AngularJS modules – How to create??
• Add controller module in myapp module
<div ng-app="myApp">...</div>
<script>
var app = angular.module("myApp", []);
Name
app.controller("myCtrl", function($scope) {
$scope.firstName = "Ajeet"; Controller
$scope.lastName = "Maurya"; }); Definition
</script>
AngularJS modules – How to create??
</div>
AngularJS modules – How to create??
<div ng-app="myApp“ ng-controller=“myctrl”>
{{ firstName + " " + lastName }}
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "Ajeet";
$scope.lastName = "Maurya"; });
</script>
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.j
s"></script>
<body>
<div ng-app="myApp” ng-controller=“myCtrl”>
{{firstName+" "+lastName}}
</div>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.firstName="Riya";
$scope.lastName="Mishra";});
</script>
</body>
</html>
Separate Javascript files for each module
app.js
var app = angular.module("myApp",[]);
ctrl.js
app.controller("myCtrl",function($scope){
$scope.firstName="Riya";
$scope.lastName="Mishra";});
Separate Javascript files for each module
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/
angular.min.js"></script>
<body>
<div ng-app="myApp" ng-
controller="myCtrl">
{{firstName+" "+lastName}}
</div>
<script src = "app.js"></script>
<script src = "ctrl.js"></script>
</body>
</html>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS expressions
• To bind application data to HTML.
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/ang
ular.min.js"></script>
<body>
<div ng-app="newapp" ng-init = "name='Reva';
PID=23;marks=[34, 45]">
{{name}}
{{PID}}
{{marks}}
</div>
<script>
var app = angular.module("newapp",[]);
</script></body></html>
ng-app ng-init ng-model ng-click
• Updates element
<div ng-app>
Readonly: <input ng-readonly="true" type="text"
value="This is readonly">
</div>
ng-app ng-init ng-model ng-click
<div ng-app>
Disabled: <input ng-disabled="true" type="text"
value="This is disabled">
</div>
ng-app ng-init ng-model ng-click
<div ng-app>
Click Me:<input type="checkbox" ng-model="checked">
New:<input ng-if = "checked" type="text">
</div>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS custom directives
• Extends the functionality of HTML.
• Defined using "directive" function.
• Replaces the element for which it is activated.
• During bootstrap finds the matching elements
• Scope of the directive
• Custom directives for following type of elements:
Element Class
Attribute Comment
AngularJS custom directives
• Make an HTML element with the same tag name as the new
directive.
• Naming a directive - camel case name,
e.g. w3TestDirective
While invoking use - separated name
w3-test-directive
Example
<body ng-app="myApp"> First Directive!!
<first-Test></first-Test>
<script>
var app = angular.module("myApp", []);
app.directive(“ firstTest ", function() {
return {
template : "<h1>First Directive!!</h1>"
};
}
);
</script>
</body>
Invoking a directive by using:
• As a tag name
<test-directive></test-directive>
• As a class name
<div class="test-directive"></div>
• As an attribute
<div test-directive></div>
• As a comment
<!-- directive: test-directive -->
Restrictions
Restrict your directives to only be invoked by some of the
methods.
By default the value is EA, meaning that both Element names and attribute
names can invoke the directive.
Example
<body ng-app="myApp"> First Directive!!
<first-Test></first-Test>
<script>
var app = angular.module("myApp", []);
app.directive(“ firstTest ", function() {
return {
template : "<h1>First Directive!!</h1>"
};
}
);
</script>
</body>
Attribute
Made by a directive!
<body ng-app="myApp">
<div w3-Test-Directive></div>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "<h1>Made by a directive!</h1>"
};
});
</script>
</body>
Class
Made by a directive!
<body ng-app="myApp">
<div class="test-Directive"></div>
<script>
var app = angular.module("myApp", []);
app.directive("testDirective", function() {
return {
restrict : "C",
template : "<h1>Made by a directive!</h1>"
};
});
</script>
</body>
Comment Made by a directive!
<body ng-app="myApp">
<!-- directive: w3-Test-Directive -->
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
restrict : "M",
template : "<h1>Made by a directive!</h1>"
};
});
</script>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
Angular JS Data Binding
• software development technologies
• Bridge between the view and business logic of the application.
• Two-Way data binding model
• One-Way Data Binding => Value is taken from the data model and inserted
into an HTML element. No way to update model from view.
Angular JS Data Binding
• Two-Way Data Binding: Automatic synchronization of data between the
model and view components.
• view is a projection of the model
• If the model is changed, the view reflects the change and vice versa.
First App – test.html
<html> <head>
<title>AngularJS First Application</title> </head>
<body> ng-app = "">
<h1>Sample Application</h1>
<div
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
<p>Hello <span ng-bind = "name"></span>!</p>
</div>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
</body>
</html>
• Open test.html in browser
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS controllers
• Control the flow of data
• ng-controller directive.
• A JavaScript object containing attributes/properties and functions.
• $scope as a parameter
AngularJS controllers
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "Aryan";
$scope.lastName = "Khanna";
});
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<br>
Full Name: {{firstName + " " + lastName}}
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "Aryan";
$scope.lastName = "Khanna";
});
</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}}
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "Aryan";
$scope.lastName = "Khanna";
});
</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>
FullName: {{fullname()}}
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "Aryan";
$scope.lastName = "Khanna";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
}; }); </script>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS scope
• Binding part between the HTML (view) and the JavaScript
(controller).
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
</script>
Root Scope
• $rootScope - created on the HTML element
• Available in the entire application.
• same variable name in both the current scope and in the rootScope
• application uses the one in the current scope
<body ng-app="myApp">
<p>The rootScope's favorite color:</p> <script>
<h1>{{color}}</h1> var app = angular.module('myApp', []);
<div ng-controller="myCtrl"> app.run(function($rootScope) {
<p>The scope of the controller's favorite $rootScope.color = 'blue';
color:</p> });
<h1>{{color}}</h1> app.controller('myCtrl',
</div> function($scope) {
<p>The rootScope's favorite color is $scope.color = "red";
still:</p> });
<h1>{{color}}</h1> </script>
Output=>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS filters
• Making the formatting and working with data easier.
• Basic Syntax:
• expressions by using the pipe (|) character
• e.g. {{ fullName | uppercase }}
Pre-built filters in AngularJS
• currency The number is formatted to currency format.
• date The date is specified to a specific format.
• filter The array is filtered on the basis of the provided criteria.
• limitTo The array or an string is limited into a specified number
of elements/characters.
• number A number if formatted to a string.
• orderBy The array is ordered by an expression.
• lowercase This filter converts a string to lowercase letters.
• uppercase This filter converts a string to uppercase letters.
• json It converts a JavaScript object into a JSON string.
Currency - Formats a number as currency.
<body>
<script>
var app = angular.module('myApp', []);
app.controller('costCtrl', function($scope) {
$scope.price = 20;
});
</script> </body>
Date Filter - Formats a date to a specified format.
<body>
<script>
var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope) {
$scope.today = new Date();
});
</script>
</body>
Filter - Display only the required objects.
Selects a subset of an array.
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<h1>filter </h1>
<ul>
<li ng-repeat="x in names | filter : 'a'">
{{ x }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', [])
App.controller('namesCtrl',
function($scope) {
$scope.names = ['Jani','Carl','Margareth','Hege' ];
});
limitTo - returns an array or a string containing
only a specified number of elements.
• Syntax=>
{{ object | limitTo : limit : begin }}
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<h1>filter </h1>
<ul>
<li ng-repeat="x in names | limitTo:3:1">
{{ x }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', [])
App.controller('namesCtrl',
function($scope) {
$scope.names = ['Jani','Carl','Margareth','Hege' ];
});
</script> </body>
orderBy Filter: Sorting an array. Strings (default
alphabetically) and numbers (default ascending)
<body>
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x in cars | orderBy">
{{x}}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.cars = ["Dodge", "Fiat", "Audi", "Volvo", "BMW", "Ford"];
});
</script>
</body>
number Filter:
Probably the simplest filter. It simply formats a number to a
string.
Syntax:
{{ string | number : fractionsize}}
<body>
<script>
var app = angular.module('myApp', []);
app.controller('nCtrl', function($scope) {
$scope.money = 999999;
});
</script>
</body>
lowercase Filter:
This filter simply converts a string to lowercase letters.
<body>
<h2>lowercase</h2>
<div ng-app="myApp" ng-controller="myCtrl">
<strong>Input:</strong>
<br>
<input type="text" ng-model="string">
<br>
<br> {{string|lowercase}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.string = "";
}); </script>
lowercase Filter:
This filter simply converts a string to uppercase letters.
<body>
<h2>Uppercase</h2>
<div ng-app="myApp" ng-controller="myCtrl">
<strong>Input:</strong>
<br>
<input type="text" ng-model="string">
<br>
<br> {{string|uppercase}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.string = "";
}); </script>
Json filter - This filter simply converts a JavaScript object into a JSON string,
and this is very much useful while the debugging of applications.
Syntax:
{{ object | json : spacing }}
<body>
<div ng-app="myApp" ng-controller="jsCtrl">
<h1>Carnames:</h1>
<pre>{{cars | json}}</pre>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('jsCtrl', function($scope) {
$scope.cars = ["Audi", "BMW", "Ford"];
});
</script>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
AngularJS dependency injection
• Divide your application into multiple different types of components.
addEmp();
Example – Code using DI
var employee = function(empName, designation){
this.empName = empName;
this.designation = designation;
}
function addEmp(){
..code
}
var emp = new employee(“John”, “Manager”);
addEmp(emp);
AngularJS dependency injection
• Core components
• Value
• Factory
• Service
• Provider
• Constant
AngularJS dependency injection
• Core components
• Value: Number, String or JavaScript object.
• Pass values in factories, services or controllers
//define a module
var myModule = angular.module("myModule", []);
//create a value object and pass it a data.
myModule.value("numberValue", 100);
myModule.value("stringValue", "abc");
myModule.value("objectValue", { val1 : 123, val2 : "abc"} );
//Define Constant
app.constant('clientId', '123456');
}]);
<body>
<div ng-app="">
<form>
Check to show a header:
<input type="checkbox" ng-model="myVar">
</form>
<h1 ng-show="myVar">My Header</h1>
</div>
Radiobuttons
<body ng-app="">
<form>
Pick a topic:
<input type="radio" ng-model="myVar" value="dogs">Dogs
<input type="radio" ng-model="myVar" value="cats">Cats
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
<div ng-switch-when="cats">
<h1>Cars</h1>
<p>Read about cats.</p> </div></div>
<body ng-app="">
Selectbox <form>
Select a topic:
<select ng-model="myVar">
<option value="">
<option value="IAT1">IAT1
<option value="IAT2">IAT2
</select>
</form>
<div ng-switch="myVar">
<div ng-switch-when="IAT1">
<h1>IAT1</h1>
<p>IAT1 Portion</p>
</div>
<div ng-switch-when="IAT2">
<h1>IAT2</h1>
<p>IAT2 Portion</p>
</div> </body>
<div ng-app="myApp" ng-controller="formCtrl">
<form novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div> <script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.master = {firstName: "John",
lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>
Points to be covered
• Overview of AngularJS
• Need of AngularJS in real web sites
• AngularJS modules, AngularJS built-in directives
• AngularJS custom directives, AngularJS expressions
• Angular JS Data Binding, AngularJS filters
• AngularJS controllers, AngularJS scope
• AngularJS dependency injection, Angular JS Services
• Form Validation
• Routing using ng-Route, ng-Repeat, ng-style, ng-view
• Built-in Helper Functions, Using Angular JS with Typescript
Angular JS Services
• Function or object
• 30 built-in services
• E.g. $location
After 3 seconds
$log Service
• Logs the messages to the browser's console.
2) Core Features
● Data-binding − It is the automatic synchronization
of data between model and view components.
● Scope − These are objects that refer to the model.
They act as a glue between controller and view.
● Controller − These are JavaScript functions bound
to a particular scope.
● Services − AngularJS comes with several built-in
services such as $http to make a
XMLHttpRequests. These are singleton objects
which are instantiated only once in app.
● Filters − These select a subset of items from an
array and returns a new array.
● Directives − Directives are markers on DOM
elements such as elements, attributes, css, and
more. These can be used to create custom HTML
tags that serve as new, custom widgets. AngularJS
has built-in directives such as ngBind, ngModel,
etc.
● Templates − These are the rendered view with
information from the controller and model. These
can be a single file (such as index.html) or
multiple views in one page using partials.
● Routing − It is concept of switching views.
● Model View Whatever − MVW is a design pattern for
dividing an application into different parts
called Model, View, and Controller, each with
distinct responsibilities. AngularJS does not
implement MVC in the traditional sense, but
rather something closer to MVVM (Model-View-
ViewModel). The Angular JS team refers it
humorously as Model View Whatever.
● Deep Linking − Deep linking allows to encode the
state of application in the URL so that it can be
bookmarked. The application can then be restored
from the URL to the same state.
● Dependency Injection − AngularJS has a built-in
dependency injection subsystem that helps the
developer to create, understand, and test the
applications easily.
ng-app –
ng-app directive defines the root element. It starts an AngularJS
Application and automatically initializes or bootstraps the application
when web page containing AngularJS Application is loaded. It is also
used to load various AngularJS modules in AngularJS Application.
ng-bind –
The ng-bind Directive in AngularJS is used to bind/replace the text content of
any particular HTML element with the value that is entered in the given
expression. The value of specified HTML content updates whenever the value of
the expression changes in the ng-bind directive.
ng-model –
The ng-model directive is a directive that is used to bind the values of the HTML
controls (input, select, and textarea) or any custom form controls, and stores the
required user value in a variable and we can use that variable whenever we require
that value.
7) AngularJS expressions.
In AngularJS, expressions are used to bind application data to HTML. AngularJS
resolves the expression, and return the result exactly where the expression is
written. Expressions are written inside double braces {{expression}}.They can
also be written inside a directive:
ng-bind="expression".
Example=>
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></scri
pt>
<body>
<div ng-app="">
{{2+3}}<br>
{{3/3}}<br>
{{2*3}}<br>
{{2-3}}
</div>
</body>
</html>
If you remove the directive "ng-app", HTML will display the expression without
solving it. You can also write expressions wherever you want, AngularJS will
resolve the expression and return the result.
AngularJS numbers are similar to JavaScript numbers.
Example=>
<div ng-app ng-init="pi=3.14;r=4">
Area of circle = {{pi*r*r}}<br>
</div>
Output=>
AngularJS Strings=>
<div ng-app ng-init="name='ABC'; roll=23">
Details = {{name+" "+roll}}<br>
</div>
Output=>
Object=>
<div ng-app ng-init="Student={name:'ABC', roll:23}">
Details = {{Student.name+" "+Student.roll}}<br>
</div>
Output=>
Array=>
<div ng-app ng-init="marks=[23, 30, 27]">
Details = {{marks[0]}}<br></div>
Output=>
8) Angular modules.
Here, "myApp" specifies an HTML element in which the application will run.
Now we can add controllers, directives, filters, and more, to AngularJS
application.
Complete code=>
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<body>
<div ng-app="myApp” ng-controller=“myCtrl”>
{{firstName+" "+lastName}}
</div>
<script>
var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope){
$scope.firstName="Riya";
$scope.lastName="Mishra";});
</script>
</body>
</html>
Output⇒
In AngularJS applications, we can put the module and the controllers in
JavaScript files too.
app.js
var app = angular.module("myApp",[]);
ctrl.js
app.controller("myCtrl",function($scope){
$scope.firstName="Riya";
$scope.lastName="Mishra";});
index.html
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></scri
pt>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
{{firstName+" "+lastName}}
</div>
<script src = "app.js"></script>
<script src = "ctrl.js"></script>
</body>
</html>
ng-model directive
The ng-model directive defines the model/variable to be used in AngularJS
Application. The ngModel directive is a directive that is used to bind the values
of the HTML controls (input, select, and textarea) or any custom form controls,
and stores the required user value in a variable and we can use that variable
whenever we require that value. It also is used during form validations. The
various form input types (text, checkbox, radio, number, email, URL, date,
datetime-local time, month, week) can be used with the ngModel directive. This
directive is supported by <input>, <select> & <textarea>.
<html>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></scri
pt>
<body>
<div ng-app="">
<input type="text" ng-model = "name" />
<H1>Hello, {{name}}</H1>
</div>
</body>
</html>
The ng-repeat directive repeats HTML elements for each item in a collection.
Angular-JS ng-repeat directive is a handy tool to repeat a set of HTML code for
a number of times or once per item in a collection of items. ng-repeat is mostly
used on arrays and objects.
<div ng-app ng-init="name=['Sachin','Dhoni','Sehwag']">
<li ng-repeat = "nm in name">
{{nm}}
</li>
</div>
Output=>
ng-click =>
The AngularJS ng-click directive facilitates you to specify custom behavior when
an element is clicked. So, it is responsible for the result what you get after
clicking.
Syntax:
<element ng-click="expression"></element>
expression: It specifies an expression that is executed when an element is
clicked.
Example
<button ng-click = "count=count+1" ng-init="count=0"> Increment </button>
<br>
<H1>The count value is {{count}}</H1>
</div>
Output=>
ng-readonly=>
The ng-readonly Directive in AngularJS is used to specify the readonly attribute
of an HTML element. The HTML element will be readonly only if the expression
inside the ng-readonly directive returns true. The ng-readonly directive is
required to change the value between true and false. In case, if the readonly
attribute is set to false, then the presence of the readonly attribute makes the
element readonly, irrespective of its value.
Syntax:
<element ng-readonly="expression">
Contents...
</element>
Example=>
<div ng-app>
Readonly: <input ng-readonly="true" type="text" value="This is readonly">
</div>
Output=>
ng-disabled
The ng-disabled Directive in AngularJS is used to enable or disable the HTML
elements. If the expression inside the ng-disabled attribute returns true then the
form field will be disabled or vice versa. It is usually applied on the form field
(i.e, input, select, button, etc). The value of the disabled attribute can’t be set to
false, that in turn, disabled the elements, regardless of their value, in the
presence of the disabled attribute in HTML.
Example=>
<div ng-app>
Disabled: <input ng-disabled="true" type="text" value="This is disabled">
</div>
ng-if directive
The ng-if Directive in AngularJS is used to remove or recreate a portion of the
HTML element based on an expression. The ng-if is different from the ng-hide
directive because it completely removes the element in the DOM rather than just
hiding the display of the element. If the expression inside it is false then the
element is removed and if it is true then the element is added to the DOM.
<div ng-app>
Click Me:<input type="checkbox" ng-model="checked">
New:<input ng-if = "checked" type="text">
</div>
• Overview of AngularJS
https://www.geeksforgeeks.org/angularjs/
https://www.w3schools.com/angular/angular_intro.asp
https://www.tpointtech.com/what-is-angularjs
First App
https://www.tpointtech.com/angularjs-first-application
• AngularJS modules
https://www.geeksforgeeks.org/angularjs-modules/?ref=lbp
https://www.w3schools.com/angular/angular_modules.asp
https://www.geeksforgeeks.org/angularjs-ng-model-directive/?ref=lbp
https://www.w3schools.com/angular/angular_directives.asp
https://www.w3schools.com/angular/angular_model.asp
https://www.w3schools.com/angular/angular_directives.asp
https://www.tutorialspoint.com/angularjs/angularjs_custom_directives.htm
https://docs.angularjs.org/guide/directive
https://www.csharp.com/UploadFile/f0b2ed/angularjs-custom-directives/
• AngularJS expressions
https://www.geeksforgeeks.org/angularjs-expressions/?ref=lbp
https://www.w3schools.com/angular/angular_expressions.asp
• Angular JS Data Binding
https://www.tpointtech.com/angularjs-data-binding
https://www.w3schools.com/angular/angular_databinding.asp
https://www.geeksforgeeks.org/angularjs-data-binding/?ref=lbp
• AngularJS filters
https://www.w3schools.com/angular/angular_filters.asp
https://www.geeksforgeeks.org/angularjs-controllers/?ref=lbp
https://www.geeksforgeeks.org/angularjs-scope/?ref=lbp
https://www.w3schools.com/angular/angular_controllers.asp
https://www.w3schools.com/angular/angular_scopes.asp
https://docs.angularjs.org/guide/di
https://www.tpointtech.com/angularjs-dependency-injection
https://www.tutorialspoint.com/angularjs/angularjs_dependency_injection.htm
• Angular JS Services
https://www.geeksforgeeks.org/angularjs-services/?ref=lbp
https://www.w3schools.com/angular/angular_services.asp
https://www.csharp.com/article/angularjs-services-with-examples/
• Form Validation
https://www.geeksforgeeks.org/angularjs-form-validation/?ref=lbp
https://www.w3schools.com/angular/angular_validation.asp
• https://www.w3schools.com/angular/angular_routing.asp
• https://www.geeksforgeeks.org/how-to-execute-a-routing-in-the-angularjs-
framework/
• https://docs.angularjs.org/api/ng/function