Angular JS PDF
Angular JS PDF
Introduction:
Before you study AngularJS, you should have a basic understanding of:
o HTML
o CSS
o JavaScript
AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.
AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
Angular JS Architecture
MVC stands for Model View Controller. It is a software design pattern for developing web applications.
It is very popular because it isolates the application logic from the user interface layer and supports
separation of concerns.
View: It is responsible for displaying all data or only a portion of data to the users. It also specifies
the data in a particular format triggered by the controller's decision to present the data.
Controller: It is responsible to control the relation between models and views. It responds to user input
and performs interactions on the data model objects. The controller receives input, validates it, and
then performs business operations that modify the state of the data model.
Let's take an address book application as an example. The model is a list of person objects, the view is a GUI window that
displays the list of people, and the controller handles actions such as "Delete person", "Add person", "Email person", etc.
Angular JS Expressions
AngularJS expressions can be written inside double braces: {{ expression }}.
AngularJS will resolve the expression, and return the result exactly where the expression is written.
AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
“ng-app” tells AngularJS that this is the root element of the AngularJS application.
If you remove the ng-app directive, HTML will display the expression as it is, without solving it:
You can only have one ng-app directive in your HTML document. If more than one ng-app directive appears, the first
appearance will be used
Syntax:
<element ng-app="modulename">
//Angular-code
</element>
Module name is optional, specifies the name of a module to load with the application
<!DOCTYPE html><html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Full Name: {{firstName + " " + lastName}} //view
</div><script>
var app = angular.module('myApp', []); //application module used to initialize an application with controller(s)
//controller defined in next line
app.controller('myCtrl', function($scope) {
$scope.firstName = "Srikanth"; //model
$scope.lastName = "Reddy"; //model
});
</script></body></html>
The AngularJS application is defined by ng-app="myApp". The application runs inside the <div>.
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. Here it is application controller
The myCtrl function is a JavaScript function.
AngularJS will invoke the controller with a $scope object.
In AngularJS, $scope is the application object (the owner of application variables and functions).
The controller creates two properties (variables) in the scope (firstName and lastName).
The ng-model directive(s) bind the input fields to the controller propertie(s) (firstName and lastName).
The first argument in module refers to an HTML element in which the application will run
The second argument in module is an array generally contains dependent modules. Without the [] parameter, you are not
creating a new module, but retrieving an existing one.
It is recommended that you load the AngularJS library either in the <head> or at the start of the <body>. This is because
calls to angular.module can only be compiled after the library has been loaded.
</body>
</html>
2) same example using ng-bind
<!DOCTYPE html>
<html>
<script src="../../ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
</body>
</html>
3)
<!DOCTYPE html>
<html>
<script src="../../ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
Or by using bind
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
6)
<div ng-app="" ng-init="myCol='lightblue'">
<input style="background-color:{{myCol}}" ng-model="myCol" value="{{myCol}}">
</div>
Angular Directives
AngularJS lets you extend HTML with new attributes called Directives.
There is a set of built-in directive in AngularJS which offers functionality to your applications. You can also define your
own directives.
Directives are special attributes starting with ng- prefix. Following are the most common directives:
o ng-app: This directive starts an AngularJS Application.
o ng-model: This directive defines the model that is variable to be used in AngularJS.
o ng-repeat: This directive repeats html elements for each item in a collection.
AngularJS Controllers
<input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "Srikanth Reddy";
});
</script>
<p>When you change the name in the input field, the changes will affect the model, and it will also affect the name
property in the controller.</p>
</body>
</html>
AngularJS Modules
An AngularJS module defines an application. The module is a container for the different parts of an application.
The module is a container for the application controllers. Controllers always belong to a module.
A module is used as a Main() method
The angular object's module() method is used to create a module.
It is also called AngularJS function angular.module
<div ng-app="myApp">...</div>
<script>
var app = angular.module("myApp", []);
</script>
Here, "myApp" specifies an HTML element in which the application will run.
Now we can add controllers, directives, filters, and more to AngularJS application.
</div>
<script>
</script>
</body>
</html>
Angular Filters
AngularJS Filters
Filter Description
currency Format a number to a currency format.
Ex: Syntax: {{ number | currency : symbol : fractionsize }}
<!DOCTYPE html>
<html>
<script src="../../ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="costCtrl">
<p>Price = {{ price | currency }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('costCtrl', function($scope) {
$scope.price = 9.99;
});
</script>
<p>The currency filter formats a number to a currency format.</p>
</body>
</html>
By setting the ng-model directive on an input field, we can use the value of the input field as an expression in a filter.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/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>
<script>
var app = angular.module('myApp', []);
app.controller('nCtrl', function($scope) {
$scope.weight = 9999;
});
</script>
<p>The weight is written with three decimals.</p>
</body>
</html>