Angular JS
Angular JS
Module v
chapter-2
Introduction,
Angular JS Expressions,
Modules,
Data Binding,
CONTENTS
Controllers,
DOM, Events,
Forms,
Validations
Step 1 : Go to Website
Node.js (nodejs.org)
Step 2: Install Node js
Step 3: Open Command
Prompt
Step 4: run commands a)
HOW TO node –v
DOWNLOAD b) npm
ANGULARJS –v
Step 5: npm install –g
@angular/cli
Step 6: run command ng
version
Step 7: install vs-code
Download Visual Studio Code
- Mac, Linux, Windows
Step Open vs-code and create a folder
1 angularjs
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.
6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>Input something in the input box:</p>
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
</body>
</html>
EXPLANATION
The ng-model directive binds the value of the input field to the
application variable name.
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.j
s">
</script>
<body
<div ng-app="" ng-init="quantity=1;cost=5">
<p>Total in dollar: {{ quantity * cost }}</p>
</div>
</body>
</html>
OUTPUT:-Total in dollar: 5
AngularJS
Strings
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></
script>
<body>
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
<p>The full name is: {{ firstName + " " + lastName }}</p>
</div>
</body>
</html>
AngularJS
Objects
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/
1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-
init="person={firstName:'John',lastName:'Doe'}">
<p>The name is {{ person.lastName }}</p>
</div>
</body>
</html>
OUTPUT:The name is
Doe
AngularJS
Arrays
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/an
gularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-
init="points=[1,15,19,2,40]">
<p>The third result is {{ points[2] }}</p>
</div>
</body>
</html>
ANGULARJS
MODULES Controllers always belong to a module.
<div ng-app="myApp">...</div>
<script>
var app = angular.module("myApp", []);
</script>
MODULES
ARE
CONTAINERS
EXAMPLE
ANGULARJS DIRECTIVES
• You can also use double braces {{ }} to display content from the
model:
<p>First name: {{firstname}}</p>
• AngularJS controllers control
the data of AngularJS
applications.
• AngularJS controllers are
regular JavaScript Objects.
• AngularJS applications are
ANGULARJS controlled by controllers.
CONTROLLERS
• The ng-controller directive
defines the application
controller.
• A controller is a JavaScript
Object, created by a
standard JavaScript object
constructor.
EXAMPLE-2
Just copy the code between the <script> tags into an external file
named personController.js:
ANGULARJS HTML DOM
The ng-disabled directive binds the application data mySwitch to the HTML
button's disabled attribute.
The ng-model directive binds the value of the HTML checkbox element to
the value of mySwitch.
• ng-keyup
• ng-mousedown
• ng-mouseenter
• ng-mouseleave
• ng-mousemove
• ng-mouseover
• ng-mouseup
• ng-paste
MOUSE EVENTS
Mouse events occur when the cursor moves over an element, in this
order:
• ng-mouseover
• ng-mouseenter
• ng-mousemove
• ng-mouseleave
when a mouse button is clicked on an element, in this order:
• ng-mousedown
• ng-mouseup
• ng-click
EX:MOUSE EVENT
TOGGLE
• If you want to show a section of HTML code when a button is clicked, and
hide when the button is clicked again, like a dropdown menu, make the
button behave like a toggle switch:
ANGULARJS FORMS
❑ Radio buttons with the same ng-model can have different values, but
only the selected one will be used
FORM EXAMPLE
AngularJS can validate input data.
AngularJS is constantly updating the state of both the form and the input fields.
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.
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
EXAMPLE