MMC105 Web Technologies Module 4 & 5 Q & A
MMC105 Web Technologies Module 4 & 5 Q & A
Web Technologies
22MCA24 June / July 2023 Module 5 (MODULE 4 & 5 IN MMC105)
Q.9.a. What is Angular JS? Explain the following Angular JS directives: (8M)
(i) ng_app (ii) ng_model (iii) ng_bind
Q.9.b. Write an Angular JS program to use expressions. (6M)
Q.9.c. Briefly discuss the use of filter in Angular JS with an example. (6M)
Q.10.a. What is Angular JS Service? Explain any three of them by using code snippet.
(10M)
Q.10.b. Write an Angular JS program to demonstrate client-side form validation. (10M)
Q.9.a. What is AngularJS? Explain the following AngularJS directives ng-app, ng-model, ng-
bind. (4M)
Q.9.c. What is AngularJS data binding? Write a AngularJS program to add a controller.
(10M)
Q.10.a. What are AngularJS filters? Write a AngularJS program to show how you can add
filters to expressions. (10M)
Q.10.b. What is a AngularJS service? Explain any 4 of them. Write a AngularJS program to
show how you can use Slocation service. (10M)
Q.9.a. What is an angular JS directive? Explain the use of the following directives with code
snippet. ??? (Directives NOT given in Question Paper) (8M).
Q.9.b. What is the use of filter in AngularJS? Explain the use of JSON, Currency, Number,
Lowercase. (8M).
Q.10.b. With code snippet, explain the use of controllers in Angular JS. (10M).
Q.9.a. What is Angular JS? Explain the following Angular JS directives: (8M)
(i) ng_app (ii) ng_model (iii) ng_bind
[22MCA24 June / July 2023, 20MCA23 Jan. / Feb. 2023]
Definition : 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 is a JavaScript framework written in JavaScript.
AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
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.
Definition : AngularJS is a structural framework for dynamic web apps. It lets you use HTML as
your template language and lets you extend HTML's syntax to express your application's
components clearly and succinctly. AngularJS's data binding and dependency injection eliminate
much of the code you would otherwise have to write.
AngularJS Directives
AngularJS directives are HTML attributes with an ng prefix.
The ng-init directive initializes AngularJS application variables.
AngularJS Example
<div ng-app="" ng-init="firstName='John'">
<p>The name is <span ng-bind="firstName"></span></p>
</div>
</body>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
</div>
</body>
</html>
Output :
Syntax
<element ng-app="modulename">
...
content inside the ng-app root element can contain AngularJS code
...
</element>
Supported by all HTML elements.
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Parameter Values
Value Description
Optional. Specifies the name of a module to load
modulename
with the application
Example
Load a module to run in the application
<div ng-app="myApp" ng-controller="myCtrl">
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
Output :
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
<input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
<p>This example shows how the ng-model directive binds the value of an input field to a variable
in the scope.</p>
</body>
</html>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Output :
The ng-model directive binds an HTML form element to a variable in the scope.
Syntax
<element ng-model="name"></element>
Supported by <input>, <select>, and <textarea> elements.
Parameter Values
Value Description
name The name of the property you want to bind to the form
field.
Example
Bind the innerHTML of the <p> element to the variable myText:
<div ng-app="" ng-init="myText='Hello World!'">
<p ng-bind="myText"></p>
</div>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p ng-bind="myText"></p>
</div>
</body>
</html>
Output :
Syntax
<element ng-bind="expression"></element>
Or as a CSS class:
<element class="ng-bind: expression"></element>
Supported by all HTML elements.
Parameter Values
Value Description
expression Specifies a variable, or an expression to evaluate.
</body>
</html>
If you remove the ng-app directive, HTML will display the expression as it is, without solving it:
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Q.9.c. Briefly discuss the use of filter in Angular JS with an example. (6M)
[22MCA24 June / July 2023, 20MCA23 Jan. / Feb. 2023]
[ Filters are used for formatting data displayed to the user. They can be used in view templates,
controllers or services. AngularJS comes with a collection of built-in filters, but it is easy to define
your own as well. ]
AngularJS Filters
Filters can be added in AngularJS to format data.
AngularJS provides filters to transform data:
currency Format a number to a currency format.
date Format a date to a specified format.
filter Select a subset of items from an array.
json Format an object to a JSON string.
limitTo Limits an array/string, into a specified number of elements/characters.
lowercase Format a string to lower case.
number Format a number to a string.
orderBy Orders an array by an expression.
uppercase Format a string to upper case.
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
Output :
The name is DOE
]
Example
<div ng-app="myApp" ng-controller="personCtrl">
</div>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</html>
Output :
The name is doe
]
<ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
});
</script>
</body>
</html>
Output :
Looping with objects:
Joe, Denmark
Birgit, Denmark
Margareth, England
Mary, England
Jani, Norway
Hege, Norway
Kai, Norway
Carl, Sweden
Gustav, Sweden
]
</div>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
var app = angular.module('myApp', []);
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
app.controller('costCtrl', function($scope) {
$scope.price = 58;
});
</script>
</body>
</html>
Output :
Price: $58.00
The currency filter formats a number to a currency format.
]
<ul>
<li ng-repeat="x in names | filter : 'i'">
{{ x }}
</li>
</ul>
</div>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<ul>
<li ng-repeat="x in names | filter : 'i'">
{{ x }}
</li>
</ul>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
<p>This example displays only the names containing the letter "i".</p>
</body>
</html>
Output :
Jani
Birgit
Kai
This example displays only the names containing the letter "i".
Jani
Carl
Margareth
Hege
Joe
Gustav
Birgit
Mary
Kai
Example
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
<ul>
<li ng-repeat="x in names | filter : test">
{{ x }}
</li>
</ul>
</div>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<ul>
<li ng-repeat="x in names | filter:test">
{{ x }}
</li>
</ul>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</script>
</body>
</html>
Output :
Jani
Carl
Margareth
Hege
Joe
Gustav
Birgit
Mary
Kai
Margareth
Mary
Kai Norway
By adding the ng-click directive on the table headers, we can run a function that changes the sorting
order of the array:
Example
<div ng-app="myApp" ng-controller="namesCtrl">
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
</script>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
You can make your own filters by registering a new filter factory function with your module:
Example
Make a custom filter called "myFormat":
<ul ng-app="myApp" ng-controller="namesCtrl">
<li ng-repeat="x in names">
{{x | myFormat}}
</li>
</ul>
<script>
var app = angular.module('myApp', []);
app.filter('myFormat', function() {
return function(x) {
var i, c, txt = "";
for (i = 0; i < x.length; i++) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
}
txt += c;
}
return txt;
};
});
app.controller('namesCtrl', function($scope) {
$scope.names = ['Jani', 'Carl', 'Margareth', 'Hege', 'Joe', 'Gustav', 'Birgit', 'Mary', 'Kai'];
});
</script>
[
<!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.filter('myFormat', function() {
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
return function(x) {
var i, c, txt = "";
for (i = 0; i < x.length; i++) {
c = x[i];
if (i % 2 == 0) {
c = c.toUpperCase();
}
txt += c;
}
return txt;
};
});
app.controller('namesCtrl', function($scope) {
$scope.names = [
'Jani',
'Carl',
'Margareth',
'Hege',
'Joe',
'Gustav',
'Birgit',
'Mary',
'Kai'
];
});
</script>
The same answer can be referred from the following web site also :
https://www.geeksforgeeks.org/what-are-filters-in-angularjs/
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Q.10.a. What is Angular JS Service? Explain any three of them by using code snippet.
(10M) [22MCA24 June / July 2023, 20MCA23 Jan. / Feb. 2023]
AngularJS Services
In AngularJS you can make your own service, or use one of the many built-in services.
What is a Service?
In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS
application.
AngularJS has about 30 built-in services. One of them is the $location service.
The $location service has methods which return information about the location of the current web page:
Example
Use the $location service in a controller:
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>This example uses the built-in $location service to get the absolute url of the page.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
</script>
</body>
</html>
Output :
The url of this page is:
https://www.w3schools.com/angular/tryit.asp?filename=try_ng_services
This example uses the built-in $location service to get the absolute url of the page.
Note that the $location service is passed in to the controller as an argument. In order to use the
service in the controller, it must be defined as a dependency.
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
The $http service is one of the most common used services in AngularJS applications. The service
makes a request to the server, and lets your application handle the response.
Example
Use the $http service to request data from the server:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the
"myWelcome" variable.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</script>
</body>
</html>
Output :
The $http service requests a page on the server, and the response is set as the value of
the "myWelcome" variable.
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{myHeader}}</h1>
</div>
<script>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</body>
</html>
Output :
This header will change after two seconds:
Hello World!
The $timeout service runs a function after a specified number of milliseconds.
***
This header will change after two seconds:
How are you today?
The $timeout service runs a function after a specified number of milliseconds.
]
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{theTime}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
}, 1000);
});
</script>
</body>
</html>
Output :
The time is:
2:24:40 PM
The $interval service runs a function every specified millisecond.
]
To use your custom made service, add it as a dependency when defining the controller:
Example
Use the custom made service named hexafy to convert a number into a hexadecimal number:
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
[
<!DOCTYPE html>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>{{hex}}</h1>
</div>
<p>A custom service with a method that converts a given number into a hexadecimal number.</p>
<script>
var app = angular.module('myApp', []);
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
</script>
</body>
</html>
Output :
The hexadecimal value of 255 is:
ff
A custom service with a method that converts a given number into a hexadecimal number.
]
Once you have created a service, and connected it to your application, you can use the service in
any controller, directive, filter, or even inside other services.
To use the service inside a filter, add it as a dependency when defining the filter:
The service hexafy used in the filter myFormat:
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp">
Convert the number 255, using a custom made service inside a custom made filter:
<h1>{{255 | myFormat}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
</script>
</body>
</html>
Output :
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Convert the number 255, using a custom made service inside a custom made filter:
ff
]
You can use the filter when displaying values from an object, or an array:
<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
<p>This filter uses a service that converts numbers into hexadecimal values.</p>
</div>
<script>
var app = angular.module('myApp', []);
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
app.controller('myCtrl', function($scope) {
$scope.counts = [255, 251, 200];
});
</script>
</body>
</html>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Output :
Required
Use the HTML5 attribute required to specify that the input field must be filled out:
Example
The input field is required:
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>
</body>
</html>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Output :
Try writing in the input field:
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form name="myForm">
<input type="email" name="myInput" ng-model="myInput">
</form>
</body>
</html>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Output :
<script>
var app = angular.module('myApp', []);
app.directive('myDirective', function() {
return {
require: 'ngModel',
link: function(scope, element, attr, mCtrl) {
function myValidation(value) {
if (value.indexOf("e") > -1) {
mCtrl.$setValidity('charE', true);
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
} else {
mCtrl.$setValidity('charE', false);
}
return value;
}
mCtrl.$parsers.push(myValidation);
}
};
});
</script>
[
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<form name="myForm">
<input name="myInput" ng-model="myInput" required my-directive>
</form>
<script>
var app = angular.module('myApp', []);
app.directive('myDirective', function() {
return {
require: 'ngModel',
link: function(scope, element, attr, mCtrl) {
function myValidation(value) {
if (value.indexOf("e") > -1) {
mCtrl.$setValidity('charE', true);
} else {
mCtrl.$setValidity('charE', false);
}
return value;
}
mCtrl.$parsers.push(myValidation);
}
};
});
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
</script>
<p>The input field must contain the character "e" to be consider valid.</p>
</body>
</html>
Output :
Example Explained:
In HTML, the new directive will be referred to by using the attribute my-directive.
In the JavaScript we start by adding a new directive named myDirective.
Remember, when naming a directive, you must use a camel case name, myDirective, but when
invoking it, you must use - separated name, my-directive.
Then, return an object where you specify that we require ngModel, which is the
ngModelController.
Make a linking function which takes some arguments, where the fourth argument, mCtrl, is
the ngModelController,
Then specify a function, in this case named myValidation, which takes one argument, this
argument is the value of the input element.
Test if the value contains the letter "e", and set the validity of the model controller to
either true or false.
At last, mCtrl.$parsers.push(myValidation); will add the myValidation function to an array of other
functions, which will be executed every time the input value changes.
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Validation Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h2>Validation Example</h2>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
</form>
<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.user = 'John Doe';
$scope.email = '[email protected]';
});
</script>
</body>
</html>
Output :
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
The HTML form attribute novalidate is used to disable default browser validation.
Example Explained
The AngularJS directive ng-model binds the input elements to the model.
The model object has two properties: user and email.
Because of ng-show, the spans with color:red are displayed only when user or email
is $dirty and $invalid.
Q.9.c. What is AngularJS data binding? Write a AngularJS program to add a controller.
(10M)
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
https://www.w3schools.com/angular/angular_databinding.asp
HTML View
The HTML container where the AngularJS application is displayed, is called the view.
The view has access to the model, and there are several ways of displaying model data in the view.
You can use the ng-bind directive, which will bind the innerHTML of the element to the specified
model property:
Example
<p ng-bind="firstname"></p>
[
<!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('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
<p>Use the ng-bind directive to bind the innerHTML of an element to a property in the data
model.</p>
</body>
</html>
Output :
John
Use the ng-bind directive to bind the innerHTML of an element to a property in the data model.
]
You can also use double braces {{ }} to display content from the model:
Example
<p>First name: {{firstname}}</p>
[
<!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('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
<p>You can use double braces to display content from the data model.</p>
</body>
</html>
Output :
First name: John
You can use double braces to display content from the data model.
]
Or you can use the ng-model directive on HTML controls to bind the model to the view.
The ng-model Directive
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Use the ng-model directive to bind data from the model to the view on HTML controls (input,
select, textarea)
Example
<input ng-model="firstname">
[
<!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('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
<p>Use the ng-model directive on HTML controls (input, select, textarea) to bind data between the
view and the data model.</p>
</body>
</html>
Output :
]
The ng-model directive provides a two-way binding between the model and the view.
Two-way Binding
Data binding in AngularJS is the synchronization between the model and the view.
When data in the model changes, the view reflects the change, and when data in the view changes,
the model is updated as well. This happens immediately and automatically, which makes sure that
the model and the view is updated at all times.
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
Example
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="firstname">
<h1>{{firstname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
[
<!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('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
<p>Change the name inside the input field, and the model data will change automatically, and
therefore also the header will change its value.</p>
</body>
</html>
Output :
Name:
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
John
Change the name inside the input field, and the model data will change automatically, and therefore
also the header will change its value.
]
AngularJS Controller
Applications in AngularJS are controlled by controllers. Read about controllers in the AngularJS
Controllers chapter.
Because of the immediate synchronization of the model and the view, the controller can be
completely separated from the view, and simply concentrate on the model data. Thanks to the data
binding in AngularJS, the view will reflect any changes made in the controller.
Example
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.changeName = function() {
$scope.firstname = "Nelly";
}
});
</script>
[
<!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('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.changeName = function() {
Answers are given in detail. Please answer to the point for the asked questions and allotted marks.
$scope.firstname = "Nelly";
}
});
</script>
<p>This example demonstrates how to use the controller to change model data.</p>
</body>
</html>
Output :
John
Click on the header to run the "changeName" function.
This example demonstrates how to use the controller to change model data.
]