How to write a Program using Angular JS.docx
How to write a Program using Angular JS.docx
<head>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3
/angular.min.js">
</script>
</head>
AngularJS Expressions
<head>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script
>
</head>
<body >
<div>
</div>
<div>
4 + 8 = {{4 + 8}}
</div>
</body>
</html>
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.
<!doctype html>
<html ng-app>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type = "text" ng-model = "yourName" placeholder = "Enter a name here">
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
2- b>ng-model, ng-init
<!DOCTYPE html>
<html ng-app>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<h2>Cost Calculator</h2>
</div>
</body>
</html>
Step 4 - Bind the value of the above model defined using ng-bind directive.
<p>Hello <span ng-bind="name"></span>!</p>
<!doctype html>
<html ng-app>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type = "text" ng-model = "yourName" placeholder = "Enter a name here">
<hr />
</div>
</body>
</html>
{{yourName}}
built-inAngularJS directives.
Directive Description
ng-app Auto bootstrap AngularJS application.
ng-init Initializes AngularJS variables
ng-model Binds HTML control's value to a property on the $scope object.
ng-control
Attaches the controller of MVC to the view.
ler
Replaces the value of HTML control with the value of specified
ng-bind
AngularJS expression.
Repeats HTML template once per each item in the specified
ng-repeat
collection.
Display HTML element based on the value of the specified
ng-show
expression.
ng-readon Makes HTML element read-only based on the value of the
ly specified expression.
ng-disable Sets the disable attribute on the HTML element if specified
d expression evaluates to true.
ng-if Removes or recreates HTML element based on an expression.
ng-click Specifies custom behavior when an element is clicked.
4-a>ng-repeat
<!DOCTYPE html>
<htmlng-app>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-init = "names=['Tom','Jerry','Donald']">
<p>ng-repeat directive:</p>
<ol>
<li ng-repeat="x in names">
{{ x }}
</li>
</ol>
</div>
</body>
</html>
4-b>ng–repeat
<!doctype html>
<html ng-app>
<head>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular
.min.js"></script>
</head>
<body>
<div ng-init = "names = [{name:'Tom',age:'25'},
{name:'Jerry',age:'30'}, {name:'Donald',age:'28'}]">
<ol>
<li ng-repeat = "x in names">
{{ 'Students Name: ' + x.name + ', Students Age: ' +
x.age }}
</li>
</ol>
</div>
</body>
</html>
Exercise: take input of first and last name, marks of 2 subjects. And display the avg.
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h2>AngularJS Enable/disabled Application</h2>
<div ng-app = "">
</div>
</body>
</html>
directives are defined using the "directive" function. A custom directive simply replaces
AngularJS provides support to create custom directives for the following type of
elements.
encountered.
6a>ng-controller
The ng-controller directive adds a controller to your application. In the
controller you can write code, and make functions and variables, which will be
parts of an object, available inside the current HTML element. In AngularJS this
object is called a scope.
<!DOCTYPE html>
<html>
<head>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myNgApp">
<h2>AngularJS Controller</h2>
<div ng-controller="myController">
{{message}}
</div>
<script>
var ngApp = angular.module('myNgApp', []);
</body>
</html>
6 –b>
<!DOCTYPE html>
<html>
<head>
<title>Angular JS Controller</title>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<script>
varmainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.student = {
firstName: "Tom",
lastName: "Jerry",
fullName: function() {
varstudentObject;
studentObject = $scope.student;
returnstudentObject.firstName + " " + studentObject.lastName;
}
};
});
</script>
</body>
</html>
7) module
ngModule in Angular refers to a place where you can group the components,
directives, pipes, and services, which are related to the 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.
<!DOCTYPE html>
<html ng-app = "myModule">
<head>
8) AngularJS Filter -
● AngularJS Filters allow us to format the data to display on UI without changing the
original format.
● Filters can be used with an expression or directives using pipe | sign.
{{expression | filterName:parameter }}
● Angular includes various filters to format data of different data types.
●
Currency -
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<script>
var app = angular.module('myApp', []);
app.controller('costCtrl', function($scope) {
$scope.price = 60;
});
</script>
</body>
</html>
2 Date
<div ng-app="myApp" ng-controller="datCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope) {
$scope.today = new Date();
});
</script>
3 Filter
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
<li ng-repeat="x in names | filter : 'i'">
{{ x }}
</li>
</ul>
</div>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
4 JSON
<div ng-app="myApp" ng-controller="jsCtrl">
{{customer | json}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('jsCtrl', function($scope) {
$scope.customer = {
"name" : "Alfreds Futterkiste",
"city" : "Berlin",
"country" : "Germany"
};
});
</script>
UpperCase LowerCase -
<div ng-app="myApp" ng-controller="personCtrl">
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
Orderby
<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>
8>Form
<!DOCTYPE html>
<html>
<head>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h2>AngularJS Form</h2>
<div ng-app = "mainApp" ng-controller = "studentController">
<div>
Enter last name:
<input name = "lastname" type = "text" ng-model = "lastName" required>
<span style = "color:red" ng-show = "studentForm.lastname.$dirty &&
studentForm.lastname.$invalid">
<span ng-show = "studentForm.lastname.$error.required">Last Name is required.</span>
</span>
</div>
<div>
Email: </td><td><input name = "email" type = "email" ng-model = "email"
length = "100" required>
<span style = "color:red" ng-show =
"studentForm.email.$dirty&&studentForm.email.$invalid">
<span ng-show = "studentForm.email.$error.required">Email is required.</span>
<span ng-show = "studentForm.email.$error.email">Invalid email address.</span>
</span>
</div>
<button ng-click = "reset()">Reset</button>
</form>
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.reset = function(){
$scope.firstName = "";
$scope.lastName = "";
$scope.email = "";
}
$scope.reset();
});
</script>
</body>
</html>
9>AJAX
Data.txt
[
{
"Name" : "Vikas",
"RollNo" : 101,
"Percentage" : "80%"
},
{
"Name" : "Pranav",
"RollNo" : 102,
"Percentage" : "70%"
},
{
"Name" : "Rohit",
"RollNo" : 103,
"Percentage" : "75%"
},
{
"Name" : "Atharv",
"RollNo" : 110,
"Percentage" : "77%"
}
]
Test.html
<html>
<head>
<title>Angular JS Ajax</title>
<table border="1">
<tr>
<th>Name</th>
<th>Roll No</th>
<th>Percentage</th>
</tr>
<script>
functionstudentController($scope,$http) {
varurl = "data.txt";
$http.get(url).then( function(response) {
$scope.students = response.data;
});
}
</script>
</body>
</html>
ng-template
ng-template directive is used to create an html view using script tag. It contains an "id" attribute
which is used by $routeProvider to map a view with a controller.
$routeProvider
$routeProvider is the key service which set the configuration of urls, map them with the
corresponding html page or ng-template, and attaches a controller with the same.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js"></script>
<body ng-app="myApp">
<p><a href="#/!">Main</a></p>
<a href="#!banana">Banana</a>
<a href="#!tomato">Tomato</a>
<p>The HTML shown in the ng-view directive are written in the template property of the
$routeProvider.when method.</p>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
template : "<h1>Main</h1><p>Click on the links to change this content</p>"
})
.when("/banana", {
template : "<h1>Banana</h1><p>Bananas contain around 75% water.</p>"
})
.when("/tomato", {
template : "<h1>Tomato</h1><p>Tomatoes contain around 95% water.</p>"
});
});
</script>
</body>
</html>
*********
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<body ng-app="myApp">
<p><a href="#/!">Main</a></p>
<a href="#!HTML">Course-1</a>
<a href="#!Javascript">Course-2</a>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.html"
})
.when("/HTML", {
templateUrl : "HTML.html"
})
.when("/Javascript", {
templateUrl : "Javascript.html"
});
});
</script>
</body>
</html>
*****************
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<body ng-app="myApp">
<p><a href="#/!">Main</a></p>
<div ng-view></div>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/london", {
templateUrl : "london.htm"
})
.when("/paris", {
templateUrl : "paris.htm"
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Dependency Injection</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.config(function($provide) {
$provide.provider('MathService', function() {
this.$get = function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
};
});
});
mainApp.value("defaultInput", 5);
mainApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
return factory;
});
mainApp.service('CalcService', function(MathService) {
this.square = function(a) {
return MathService.multiply(a,a);
}
});
mainApp.controller('CalcController', function($scope, CalcService,
defaultInput) {
$scope.number = defaultInput;
$scope.result = CalcService.square($scope.number);
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
</script>
</body>
</html>
<body>
<h2>AngularJS Sample Application</h2>
<script>
varmainApp = angular.module("mainApp", []);
mainApp.directive('student', function() {
var directive = {};
directive.restrict = 'E';
directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";
directive.scope = {
student : "=name"
}
return directive;
});
mainApp.controller('StudentController', function($scope) {
$scope.Mahesh = {};
$scope.Mahesh.name = "Mahesh Parashar";
$scope.Mahesh.rollno = 1;
$scope.Piyush = {};
$scope.Piyush.name = "PiyushParashar";
$scope.Piyush.rollno = 2;
});
</script>
</body>
</html>