WT Practicals ALL
WT Practicals ALL
Node is an environment in which you can run JavaScript code "Outside the web browser".
These are basically variables which store some data and can be accessed from anywhere in
your code – doesn't matter how deeply nested the code is.
Commonly used Global variables:
• __dirname: This variable stores the path to the current working directory.
• __filename: This variable stores the path to the current working file.
Create a new file called app.js and open up a new integrated VS Code Terminal.
Paste the following code in the app.js file and save it:
// __dirname Global Variable
console.log(__dirname);
Modules in NodeJS
In Node.js, a module is essentially a reusable block of code that can be used to perform a
specific set of tasks or provide a specific functionality. A module can contain variables,
functions, classes, objects, or any other code that can be used to accomplish a particular task
or set of tasks.
This is the code in hello.js file:
function sayHello(name){
console.log(`Hello ${name}`);
}
module.exports = sayHello
This is the code in app.js file:
const sayHello = require('./hello.js');
sayHello('John');
sayHello('Peter');
sayHello('Rohit');
The file hello.js can be called the module in this case. Every module has an object
called exports which should contain all the stuff you want to export from this module like
variables or functions. In our case, we are defining a function in the hello.js file and directly
exporting it.
The app.js file imports the sayHello() function from hello.js and stores it in
the sayHello variable. To import something from a module, we use the require() method
which accepts the path to the module. Now we can simply invoke the variable and pass a
name as a parameter. Running the code in app.js file will produce the following output:
Hello John
Hello Peter
Hello Rohit
Built In modules in Nodejs
The OS Module
The OS Module (as its name implies) provides you methods/functions with which you can
get information about your Operating System.
const os = require('os')
// os.uptime()
const systemUptime = os.uptime();
// os.userInfo()
const userInfo = os.userInfo();
const pathInfo = {
fileName: path.basename(myPath),
folderName: path.dirname(myPath),
fileExtension: path.extname(myPath),
absoluteOrNot: path.isAbsolute(myPath),
detailInfo: path.parse(myPath),
}
• Deleting a file
• Renaming a file
// Import fs module
const fs = require('fs');
const fs = require('fs');
try{
// Write to file synchronously
fs.writeFileSync('./myFolder/myFileSync.txt', 'myFileSync says Hi');
console.log('Write operation successful');
} catch(err){
console.log('Error occurred!');
console.log(err);
}
<body ng-app>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js">
</script>
<h1>Sample Application</h1>
Enter Numbers to Multiply:
<input type="text" ng-model="Num1" /> x <input
type="text" ng-model="Num2" />
= <span>{{Num1 * Num2}}</span>
</body>
</html>
PRACTICAL 3B:
Write a program to display your name with welcome note :HELLO
><!doctype html>
<html ng-app>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/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>
PRACTICAL 4A: Demonstarte controllers in angularjs
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<body>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
</div>
</body>
</html>
</body>
</html>
5CConditional directive
<!DOCTYPE html>
<html>
<head>
<title>
AngularJs ng-if Directive Example</title>
<script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"> </script>
<script>
var app = angular.module("AngularngifApp", []);
app.controller("ngifctrl", function ($scope) {
$scope.items = ['one', 'two', 'three', 'four'];
$scope.selectval = "two";
});
</script>
</head>
<body ng-app="AngularngifApp">
<div ng-controller="ngifctrl">
Select Item: <select ng-model="selectval" ng-options="x for x in items">
</select>
<p> You have selected <span ng-bind="selectval"</span></p>
<br /><br />
<div ng-if="selectval=='one'">
<b>Show Div If Selected Value One</b>
</div>
</div>
<input type="checkbox" ng-model="showDiv" />
<label for="showDiv"> Check it </label>
<br><br>
<div class="nk" ng-if="showDiv">
hello stduents good mroning
</div>
</body>
</html>
<!DOCTYPE html>
<html >
<head>
<title>ng-style</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<style type="text/css">
.bg-color {
background-color: orange;
}
</style>
</head>
<body ng-app="app">
<h1 ng-controller="myCtrl" ng-style="myObj">Welcome</h1>
<div ng-controller="controllerName">
<input type="text" ng-model="color" placeholder="Enter any color." />
<p ng-repeat="record in records" ng-style="{color:color}">
<span class="bg-color"> Name: {{record.name}}</span>
</p>
</div>
<script>
var app = angular.module("app", []);
app.controller('controllerName', ['$scope', function ($scope) {
$scope.records = [{ name: 'Java Script' }, { name: 'AngularJS' }, { name: 'Jquery' }, {
name: 'Bootstrap' }];
}]);
app.controller("myCtrl", function($scope) {
$scope.myObj = {
"color" : "white",
"background-color" : "coral",
"font-size" : "60px",
"padding" : "50px"
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html >
<head>
<title>ng-click</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
</head>
<body ng-app="clickExample">
<div ng-controller="ExampleController">
<a href="# ng-click="showAlert()">Click Here </a>
</div>
<script>
var app = angular.module("clickExample", []);
app.controller('ExampleController', ['$scope', function ($scope) {
$scope.showAlert = function () {
alert("This is an example of ng-click");
}
}]);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>ng-mouseover</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<style>
.nav {
padding: 10px;
cursor: pointer;
background-color: gray;
border-bottom: 5px solid tomato;
}
</style>
</head>
<body ng-app="app">
<div ng-controller="controllerName">
<span class="nav" ng-repeat="p in nav" ng-style="style" ng-
mouseover="style={'border-bottom': '5px solid red','background-
color':'pink','cursor':'pointer'}" ng-mouseleave="style={}">{{p}}</span>
</div>
<script>
var app = angular.module("app", []);
app.controller('controllerName', ['$scope', function ($scope) {
$scope.nav=["Home","About","Contact"]
}]);
</script>
</body>
</html>
5F Keyboard directives
<!DOCTYPE html>
<html >
<head>
<title>ng-keyup</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
</head>
<body ng-app>
<div>
Type in text box to update count value.<br />
<input type="text" ng-init="count=0" ng-keyup="count=count+1" /><br />
Count:{{count}}
</div>
</body>
</html>
PRACTICAL 6:
Demonstrate Input Validation features of Angular.js forms with a
program
Input fields have the following states:
• $untouched The field has not been touched yet
• $touched The field has been touched
• $pristine The field has not been modified yet
• $dirty The field has been modified
• $invalid The field content is not valid
• $valid The field content is valid
They are all properties of the input field, and are either true or false.
Form State:
Forms have the following states:
• $pristine No fields have been modified yet
• $dirty One or more have been modified
• $invalid The form content is not valid
• $valid The form content is valid
• $submitted The form is submitted
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Welcome in the Angular JS</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
</head>
<body ng-app>
<div>
<form name="sampleForm">
<input type="text" ng-model="name" name="name" required />
<span ng-show="sampleForm.name.$error.required" class="text-danger">This field is
required !</span><br />
<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
$scope.products = ["Milk", "Bread", "Cheese"];
$scope.addItem = function () {
$scope.errortext = "";
if (!$scope.addMe) {return;}
if ($scope.products.indexOf($scope.addMe) == -1) {
$scope.products.push($scope.addMe);
} else {
$scope.errortext = "The item is already in your shopping list.";
}
}
$scope.removeItem = function (x) {
$scope.products.splice(x, 1);
}
});
</script>
<p>Try to add the same item twice, and you will get an error message.</p>
</body>
</html>
PRACTICAL 8:
Write a Angular.js program to implement the concept of Single
page application
<!DOCTYPE html>
<!--ng-app directive tells AngularJS that myApp
is the root element of the application -->
<html ng-app="myApp">
<head>
<!--import the angularjs libraries-->
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular-route.min.js">
</script>
<style>
body {
text-align: center;
}
</style>
</head>
<body>
<h1>Practical 8</h1>
<h3>Single Page Application in AngularJS</h3>
<a href="#/">First</a>
<a href="#/second">Second</a>
<a href="#/third">Third</a>
<div ng-view></div>
<script>
var app = angular.module('myApp', []);
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'first.html',
controller: 'FirstController'
})
.when('/second', {
templateUrl: 'second.html',
controller: 'SecondController'
})
.when('/third', {
templateUrl: 'third.html',
controller: 'ThirdController'
})
</html>
PRACTICAL 9:AngularJS Filters
9A uppercase
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<p>The name is {{ lastName | uppercase }}</p>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
9b order-by
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p>Looping with objects:</p>
<ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</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'}
];
});
</script>
</body>
</html>
9c currency
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="costCtrl">
<h1>Price: {{ price | currency:"₹"}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('costCtrl', function($scope) {
$scope.price = 58;
});
</script>
<p>The currency filter formats a number to a currency format.</p>
</body>
</html>
9d filter
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.3/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="namesCtrl">
<p>Type a letter in the input field:</p>
<p><input type="text" ng-model="test"></p>
<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'
];
});
</script>
<p>The list will only consists of names matching the filter.</p>
</body>
</html>