Question Bank-1
Question Bank-1
Manipulate DOM — Controllers should contain only business logic. Putting any
presentation logic into Controllers significantly affects its testability. AngularJS
has data binding for most cases and directives to encapsulate manual DOM
manipulation.
Format input — Use AngularJS form controls instead.
Filter output — Use AngularJS Built-in filters instead.
Share code or state across controllers — Use AngularJS services instead.
Manage the life-cycle of other components (for example, to create service
instances).
b) Develop an AngularJS code snippet using Controllers and $scope to create a simple
web page to display the concatenation of the First name and Second name as input of a
user.
<!DOCTYPE html>
<html>
<head>
<title>ng-controller Directive</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = " ";
$scope.lastName = " ";
});
</script>
</head>
<body>
<center>
<h1 style="color:green">GeeksforGeeks</h1>
<h2>ng-controller Directive</h2>
<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>
</center>
</body>
</html>
Output:
2. a) Describe the function responsible for invoking the notification process of the framework
in AngularJS.
During the framework initialization, the compiler walks through the DOM tree
looking for directives. When it finds the ngModel directive attached to any kind of
input field, it binds its own scope's $apply function to the onkeydown event. This
function is responsible for invoking the notification process of the framework called
the digest cycle. The terms related to Digest cycle are AngularJs watch, watch counts, and
watch list.
AngularJs watch: It is provided by AngularJs Framework to keep track of scope variables
and the change in their values.
Watch counts: Angular performs watching on data-binded variables but if we want we can
perform watch on normal variables as well, using watch function. It takes parameter the
variable that we explicitly want to watch.
Watch list: Maintains a list of all the watches associated with an angular application. i.e all
the data bindings being monitored.
b) Develop an AngularJS using custom filters to create a simple web page with a search box
that, when used, displays a list of items that have been pre-specified in the code.
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS Filters Example</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
</head>
<body>
<div ng-controller="myController">
<input type="text" ng-model="searchText" placeholder="Search">
<ul>
<li ng-repeat="item in items | filter: searchText">{{ item.name }}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.items = [
{ name: 'Item 1' },
{ name: 'Item 2' },
{ name: 'Item 3' },
{ name: 'Another Item' },
// ... more items
];
});
</script>
</body>
</html>
Output:
3. a) How can you create a custom directive in AngularJS that, when utilized, outputs
information about a student, such as their name and roll number in the below format
Specify the steps involved in implementing and using custom directives for this specific
purpose.
<html>
<head>
<title>Angular JS Custom Directives</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
$scope.Piyush = {};
$scope.Piyush.name = "Piyush Parashar";
$scope.Piyush.rollno = 2;
});
</script>
</body> </html>
Output:
4 a) How can you demonstrate the implementation of an Ajax service in AngularJS to set the
response for students with the highest priority in a student record? Provide a step-by-step
illustration of the code or configuration required to achieve this functionality.
We have the file data.txt, which will include the records of the student. An Ajax call will be
made by the $http service. It is going to divert & set the response to the students having
priority. After this extraction, the tables will be drawn up in the HTML, which will be based
on the students model.
.Code
<!DOCTYPE html>
<html>
<head>
<title>AngularJS AJAX - $http</title>
<style>
table, th, td {
border: 1px #2E0854;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #F6ADCD;
}
table tr:nth-child(even) {
background-color: #42C0FB;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
</head>
<body>
<center>
<h1 style="color:green">GeeksforGeeks</h1>
<h3>AJAX - $http</h>
<div ng-app="" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Goals</th>
<th>Ratio</th>
</tr>
<tr ng-repeat="student in students">
<td>{{ Player.Name }}</td>
<td>{{ Player.Goals}}</td>
<td>{{ Player.Ratio}}</td>
</tr>
</table>
</div>
<script>
function studentController($scope, $http) {
var url = "/data.txt";
$http.get(url).then( function(response) {
$scope.students = response.data;
});
}
</script>
</center>
</body> </html>
Error handling with an Angular promise involves using the .catch() method to
handle errors occurring during asynchronous operations.
When working with promises, you can attach an error handler using
the .catch() method.
This allows you to centralise the error-handling logic instead of scattering it across
multiple callbacks.
If an error occurs during the execution of a promise chain, the control jumps to
the nearest .catch() callback, where you can handle the error appropriately.
$http.get('https://api.example.com/data')
.then(function(response) {
var processedData = response.data;
return processedData;
})
.catch(function(error) {
console.log('Error:', error);
});
b) Explore the various techniques employed within the AngularJS $http service for initiating
a request to the server and receiving a response. Provide insights into the methods and
functionalities utilized during this process in AngularJS.
The AngularJS $http service makes a request to the server, and returns a response.
$http.get
$http.post
$http.head
$http.put
$http.delete
$http.patch
Parameters
Returns
Parameters
Parameters
Returns
$http.delete:
delete(url, [config]); Shortcut method to perform DELETE request.
Parameters
Parameters
Returns
The $http service, which serves the task for reading all the data that is available on the
remote servers. $http.json is one of the method used to serve this purpose.
Returns
b) Examine the approaches employed within the AngularJS $q service for executing
functions asynchronously and effectively, utilizing their return values.
$q is a built-in service which helps in executing asynchronous functions, and use their return
values (or exception) when they are finished with processing.
The below methods are employed for asynchronous execution of the functions.
Methods
resolve(value) – resolves the derived promise with the value. If the value is a
rejection constructed via $q.reject, the promise will be rejected instead.
Use $q.when() or $q.resolve() when you want to immediately resolve a
promise from a non-promise Object.
reject(reason) – Using this, it will immediately reject a promise and this comes in
handy for things such as HTTP Interceptors at the point of no return to return, so
we can just return a rejected promise Object. Resolving with a rejection
constructed via $q.reject.
notify(value) - provides updates on the status of the promise's execution. This may
be called multiple times before the promise is either resolved or rejected.
7. Create an AngularJS webpage that utilizes built-in services to perform addition of two
numbers.
<!DOCTYPE html>
<html ng-app="additionApp">
<head>
<title>AngularJS Addition</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></
script>
</head>
<body ng-controller="additionController">
<h2>AngularJS Addition</h2>
<script>
var app = angular.module('additionApp', []);
$scope.addNumbers = function () {
$scope.result = $scope.number1 + $scope.number2;
};
});
</script>
</body>
</html>
8. Create an AngularJS webpage that uses Dependency Injecting services like Factory, Value
and Service to perform the Square of a number.
<html>
<head>
<title>Angular JS Services</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>
var mainApp = angular.module("mainApp", []);
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) {
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
</script>
</body>
</html>
OUTPUT: