Professional Documents
Culture Documents
MST unit2
MST unit2
UNIT-2
JAVASCRIPT
Definition of Java-script:
Example Code
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function changeColor() {
document.getElementById("text").style.color = "red";
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
}
</script>
</head>
<body>
</body>
</html>
JavaScript isn't limited to just changing colors; it can do a lot more. It's
used for things like form validation (checking if users entered correct
information), creating animations, fetching data from servers without
reloading the page (using AJAX), building interactive games, and much
more.
Advantages of JavaScript:
Disadvantages of JavaScript:
- Definition:
// Creating an object
let person = {
name: "John",
age: 30,
country: "USA"
};
console.log(person.age); // Output: 30
person.age = 35;
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
console.log(person.age); // Output: 35
Definition:
// Arithmetic operations
let x = 5;
let y = 3;
// String concatenation
3. Control Statements:
- Definition:
- Example 1: If statement.
// If statement
if (num > 0) {
console.log("Positive number");
} else {
console.log("Negative number");
// For loop
// Output:
// 1
// 2
// 3
// 4
// 5
4. Arrays:
// Creating an array
fruits[2] = "grape";
5. Functions:
// Defining a function
function greet(name) {
function add(x, y) {
return x + y;
6. Constructors:
// Constructor function
this.name = name;
this.age = age;
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
}
// Creating objects
console.log(person1.age); // Output: 30
function Circle(radius) {
this.radius = radius;
this.area = function() {
};
// Creating object
Sure! Let's break down each concept in the context of AngularJS and
provide simple examples:
TOPIC 3:
1. AngularJS Expressions:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.
js"></script>
<body>
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
<div ng-app="">
</div>
<script>
// AngularJS controller
});
</script>
</body>
</html>
<div ng-app="">
<p>5 + 3 = {{ 5 + 3 }}</p>
</div>
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
2. Arrays and Objects:
<div ng-app="">
<div ng-controller="myCtrl">
</div>
</div>
<script>
});
</script>
<div ng-app="">
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
<div ng-controller="myCtrl">
</div>
</div>
<script>
});
</script>
3. $eval:
<div ng-app="">
<div ng-controller="myCtrl">
</div>
<script>
$scope.$eval = function(expression) {
return eval(expression);
};
});
</script>
<div ng-app="">
<div ng-controller="myCtrl">
</div>
</div>
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
<script>
$scope.changeName = function() {
$scope.$eval("person.name = 'Alice'");
};
});
</script>
4. Strings:
<div ng-app="">
</div>
<div ng-app="">
<div ng-controller="myCtrl">
</div>
</div>
<script>
$scope.reverseString = function(str) {
return str.split('').reverse().join('');
};
});
</script>
<span ng-show="myForm.email.$error.required">Email is
required.</span>
</form>
<button type="submit">Submit</button>
</form>
<script>
$scope.submitForm = function() {
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
alert("Form submitted successfully! Name: " + $scope.name);
};
});
</script>
Topic 4:
What is AngularJS?
Let's create a simple SPA with AngularJS that consists of two views -
Home and About.
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.
js"></script>
</head>
<body>
<div ng-view></div>
<script>
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "home.html"
})
.when("/about", {
templateUrl : "about.html"
});
});
</script>
</body>
</html>
home.html
about.html
<h1>About Us</h1>
Output:
When you navigate to the root URL, you'll see the home page content.
When you navigate to `/about`, it'll display the about page content
without reloading the entire page.
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div ng-view></div>
<script>
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "home.html",
controller: "HomeController"
})
.when("/about", {
templateUrl : "about.html",
controller: "AboutController"
});
});
MEAN STACK TECHNOLOGIES
UNIT-2
JAVASCRIPT
app.controller("HomeController", function($scope) {
});
app.controller("AboutController", function($scope) {
});
</script>
</body>
</html>
home.html
about.html
Output:
Both home and about pages will display a message fetched from their
respective controllers without reloading the page.