Angular JS Notes
Angular JS Notes
<script src="https://ajax.googleapis.com/ajax/libs/angularjs
/1.6.9/angular.min.js"></script>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
</body>
</html>
Example explained:
AngularJS Example
<div ng-app="" ng-init="firstName='John'">
</div>
AngularJS Example
<div data-ng-app="" data-ng-init="firstName='John'">
</div>
You can use data-ng-, instead of ng-, if you want to make your
page HTML valid.
AngularJS Expressions
AngularJS expressions are written inside double braces:
{{ expression }}.
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs
/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p>{{name}}</p>
</div>
</body>
</html>
AngularJS Applications
AngularJS Example
<div ng-app="myApp" ng-controller="myCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>
AngularJS Module
var app = angular.module('myApp', []);
Example
<div ng-app="" ng-init="quantity=1;cost=5">
</div>
AngularJS strings are like JavaScript strings:
Example
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
</div>
Example
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
</div>
AngularJS Objects
AngularJS objects are like JavaScript objects:
Example
<div ng-app="" ng-
init="person={firstName:'John',lastName:'Doe'}">
</div>
Same example using ng-bind:
Example
<div ng-app="" ng-
init="person={firstName:'John',lastName:'Doe'}">
</div>
AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
Example
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
Example
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
AngularJS Modules
An AngularJS module defines an application.
Creating a Module
A module is created by using the AngularJS
function angular.module
<div ng-app="myApp">...</div>
<script>
</script>
Adding a Controller
Add a controller to your application, and refer to the controller
with the ng-controller directive:
Example
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>
<script>
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
AngularJS Directives
AngularJS directives are extended HTML attributes with the
prefix ng-.
Example
<div ng-app="" ng-init="names=['Jani','Hege','Kai']">
<ul>
<li ng-repeat="x in names">
{{ x }}
</li>
</ul>
</div>
Create New Directives
In addition to all the built-in AngularJS directives, you can create
your own directives.
Example
<body ng-app="myApp">
<w3-test-directive></w3-test-directive>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "<h1>Made by a directive!</h1>"
};
});
</script>
</body>
The binding goes both ways. If the user changes the value inside
the input field, the AngularJS property will also change its value:
The ng-model directive can provide type validation for application
data (number, e-mail, required):
If the property in the ng-model attribute does not exist, AngularJS
will create one for you.
Example
<form ng-app="" name="myForm" ng-init="myText =
'post@myweb.com'">
Email:
<input type="email" name="myAddress" ng-
model="myText" required>
<h1>Status</h1>
{{myForm.myAddress.$valid}}
{{myForm.myAddress.$dirty}}
{{myForm.myAddress.$touched}}
</form>
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.
AngularJS Controllers
Just copy the code between the <script> tags into an external file
named personController.js
AngularJS Scope
The scope is the binding part between the HTML (view) and the
JavaScript (controller).
The scope is available for both the view and the controller.
How to Use the Scope?
Example
<h1>{{carname}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
});
</script>
In the view, you do not use the prefix $scope, you just refer to a
property name, like {{carname}}.
Root Scope
All applications have a $rootScope which is the scope created on
the HTML element that contains the ng-app directive.
If a variable has the same name in both the current scope and in
the rootScope, the application uses the one in the current scope.
AngularJS Filters
AngularJS provides filters to transform data:
</div>
Example
<ul>
<li ng-repeat="x in names | orderBy:'country'">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
What is a Service?
In AngularJS, a service is a function, or object, that is available
for, and limited to, your AngularJS application.
Example
AngularJS $http
.delete()
.get()
.head()
.jsonp()
.patch()
.post()
.put()
Properties
The response from the server is an object with these properties:
Example
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm")
.then(function(response) {
$scope.content = response.data;
$scope.statuscode = response.status;
$scope.statustext = response.statusText;
});
});
http is an XMLHttpRequest object for requesting external data.
AngularJS Tables
The ng-repeat directive is perfect for displaying tables.
AngularJS Example
<table>
<tr ng-repeat="x in names">
<td>{{ $index + 1 }}</td>
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
AngularJS Events
You can add AngularJS event listeners to your HTML elements by
using one or more of these directives:
ng-blur
ng-change
ng-click
ng-copy
ng-cut
ng-dblclick
ng-focus
ng-keydown
ng-keypress
ng-keyup
ng-mousedown
ng-mouseenter
ng-mouseleave
ng-mousemove
ng-mouseover
ng-mouseup
ng-paste
1. ng-mouseover
2. ng-mouseenter
3. ng-mousemove
4. ng-mouseleave
1. ng-mousedown
2. ng-mouseup
3. ng-click
Toggle, True/False
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
$event Object
You can pass the $event object as an argument when calling the
function.
Example
<div ng-app="myApp" ng-controller="myCtrl">
Checkbox
A checkbox has the value true or false. Apply the ng-
model directive to a checkbox, and use its value in your
application.
Example
Radiobuttons
Bind radio buttons to your application with the ng-model directive.
Radio buttons with the same ng-model can have different values,
but only the selected one will be used.
AngularJS Form Validation
AngularJS can validate input data.
Required
Use the HTML5 attribute required to specify that the input field
must be filled out:
Example
Example
They are all properties of the form, and are either true or false.
Comparing objects
Iterating objects
Converting data
The Global API functions are accessed using the angular object.
API Description
AngularJS Includes
With AngularJS, you can include HTML content using the ng-
include directive:
Example
<body ng-app="">
<div ng-include="'myFile.htm'"></div>
</body>
Example :
<body ng-app="myApp">
<div ng-
include="'https://tryit.w3schools.com/angular_include.php'">
</div>
<script>
var app = angular.module('myApp', [])
app.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
'https://tryit.w3schools.com/**'
]);
});
</script>
</body>
AngularJS Animations
What is an Animation?
Example:
<div ng-hide="myCheck"></div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs
/1.6.9/angular-animate.js"></script>
<body ng-app="ngAnimate">
Example
<body ng-app="myApp">
<div ng-hide="myCheck"></div>
<script>
var app = angular.module('myApp', ['ngAnimate']);
</script>
What Does ngAnimate Do?
ng-show
ng-hide
ng-class
ng-view
ng-include
ng-repeat
ng-if
ng-switch
The other directives adds a ng-enter class value when they enter
the DOM, and a ng-leave attribute when they are removed from
the DOM.
The ng-repeat directive also adds a ng-move class value when the
HTML element changes position.
ng-animate
ng-hide-animate
ng-hide-add (if the element will be hidden)
ng-hide-remove (if the element will be showed)
ng-hide-add-active (if the element will be hidden)
ng-hide-remove-active (if the element will be showed)
CSS Transitions
Example:
When the DIV element gets the .ng-hide class, the transition will
take 0.5 seconds, and the height will smoothly change from
100px to 0:
<style>
div {
transition: all linear 0.5s;
background-color: lightblue;
height: 100px;
}
.ng-hide {
height: 0;
}
</style>
CSS Animations
Example:
div {
height: 100px;
background-color: lightblue;
}
div.ng-hide {
animation: 0.5s myChange;
}
</style>
AngularJS Routing
What do I Need?
To make your applications ready for routing, you must
include the AngularJS Route module:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs
/1.6.9/angular-route.js"></script>
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/red", {
templateUrl : "red.htm"
})
.when("/green", {
templateUrl : "green.htm"
})
.when("/blue", {
templateUrl : "blue.htm"
});
});
$routeProvider
Template
You can also use the template property, which allows you to write
HTML directly in the property value, and not refer to a page.
You can also use the otherwise method, which is the default route
when none of the others get a match.