56 Angular JS Module Controller Scope Object Filters Event Handling Form Handling 02-03-2023
56 Angular JS Module Controller Scope Object Filters Event Handling Form Handling 02-03-2023
doctype html><html><head>
<script src = Include AngularJS
"https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js
"></script></head>
Point to AngularJS app
<body ng-app = "myapp">
<div ng-controller = "HelloController" >
<h2>Welcome {{helloTo.title}} to the world of Web
Technology!</h2> View
</div><script>
angular.module("myapp", []).controller("HelloController",
function($scope) {
$scope.helloTo = {}; Controller
$scope.helloTo.title = "AngularJS";
});</script></body></html>
40 R.Vijayani / Asso Prof / SITE / VIT
Include AngularJS
included the AngularJS JavaScript file in the HTML page so we can use
AngularJS
Point to AngularJS app
tell what part of the HTML contains the AngularJS app either add it
to html element or body element
View
ng-controller tells AngularJS what controller to use with this
view. helloTo.title tells AngularJS to write the "model" value named
helloTo.title to the HTML at this location.
Controller
code registers a controller function named HelloController in the angular
module named myapp.
The $scope parameter passed to the controller function is the model. The
controller function adds a helloTo JavaScript object, and in that object it
adds a title field.
41 R.Vijayani / Asso Prof / SITE / VIT
When the page is loaded in the browser,
following things happen:
HTML document is loaded into the browser, and evaluated by the
browser.
AngularJS JavaScript file is loaded, the angular global object is created.
Next, JavaScript which registers controller functions is executed.
Next AngularJS scans through the HTML to look for AngularJS apps
and views.
Once view is located, it connects that view to the corresponding
controller function.
Next, AngularJS executes the controller functions.
It then renders the views with data from the model populated by the
controller.The page is now ready.
42 R.Vijayani / Asso Prof / SITE / VIT
AngularJS $Scope
It is the binding part between the HTML (view) and the JavaScript
(controller).
It is an object with the available properties and methods.
It is available for both the view and the controller.
If we consider an AngularJS application to consist of:
View, which is the HTML.
Model, which is the data available for the current view.
Controller, which is the JavaScript function that
makes/changes/removes/controls the data.
Then the scope is the Model.
The scope is a JavaScript object with properties and methods,
which are available for both the view and the controller.
45
R.Vijayani / Asso Prof / SITE / VIT
AngularJS Filters
Filters can be added in AngularJS to format or modify 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.
Adding Filters to Expressions
Filters can be added to expressions by using the pipe character |,
followed by a filter.
56
R.Vijayani / Asso Prof / SITE / VIT
Radio
Pick a topic:
<input type="radio" ng-model="myVar" value="dogs">Dogs
<input type="radio" ng-model="myVar" value="tuts">Tutorials
<input type="radio" ng-model="myVar" value="cars">Cars
<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="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
57
R.Vijayani / Asso Prof / SITE / VIT
AngularJS Select Boxes
Creating a Select Box Using ng-options
If you want to create a dropdown list, based on an object or
an array in AngularJS, you should use the ng-options
directive:
ng-options vs ng-repeat
You can also use the ng-repeat directive to make the same
dropdown list
<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="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
61
R.Vijayani / Asso Prof / SITE / VIT
Form Validation