AngularJS

50
in.linkedin.com/in/ vipinmundayad

Transcript of AngularJS

Page 1: AngularJS

in.linkedin.com/in/vipinmundayad

Page 2: AngularJS

Contents

Introduction

General Features

Core Features

Concepts

Environment Setup

Model View Controller

Parts Of An AngularJS Application

Creating AngularJS Application

Executing AngularJS Application

How AngularJS Integrates with HTML

AngularJS Directives

AngularJS Expressions

AJS Controllers

AngularJS Filters

AngularJS $http

AngularJS Tables

AngularJS SQL

AngularJS HTML DOM

AngularJS Modules

AngularJS Forms

AngularJS Input Validation

AngularJS Includes

Advantages of AngularJS

Disadvantages of AngularJS

2

Page 3: AngularJS

Introduction

AngularJS is a very powerful JavaScript library.

It is used in Single Page Application (SPA) projects.

It extends HTML DOM with additional attributes and makes it more responsive to user actions.

AngularJS is open source, completely free, and used by thousands of developers around the world.

It is licensed under the Apache license version 2.0.

3

Page 4: AngularJS

Introduction

Definition of AngularJS as put by its official documentation is as follows:

is a structural framework for dynamic web applications. It lets you useHTML as your template language and lets you extend HTML's syntax to expressyour application components clearly and succinctly. Its data binding anddependency injection eliminate much of the code you currently have to write. Andit all happens within the browser, making it an ideal partner with any servertechnology.

4

Page 5: AngularJS

General Features

AngularJS is a efficient framework that can create Rich Internet Applications (RIA).

AngularJS provides developers an options to write client side applications using JavaScript in a clean Model View Controller (MVC) way.

Applications written in AngularJS are cross-browser compliant. AngularJS automatically handles JavaScript code suitable for each browser.

AngularJS is open source, completely free, and used by thousands of developers around the world. It is licensed under the Apache license version 2.0.

Overall, AngularJS is a framework to build large scale, high-performance, and easy-to-maintain web applications.

5

Page 6: AngularJS

Core Features

Data-binding:

It is the automatic synchronization of data between model and view components.

Scope:

These are objects that refer to the model. They act as a glue between controller and view.

Controller:

These are JavaScript functions bound to a particular scope.

6

Page 7: AngularJS

Core Features

Services:

AngularJS comes with several built-in services such as $http to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app.

Filters:

These select a subset of items from an array and returns a new array.

Directives:

Directives are markers on DOM elements such as elements, attributes, css, and more. These can be used to create custom HTML tags that serve as new, custom widgets. AngularJS has built-in directives such as ngBind, ngModel, etc.

7

Page 8: AngularJS

Core Features

Templates:

These are the rendered view with information from the controller and model. These can be a single file (such as index.html) or multiple views in one page using partials.

Routing:

It is concept of switching views.

8

Page 9: AngularJS

Core Features

Model View Whatever:

MVW is a design pattern for dividing an application into differentparts called Model, View, and Controller, each with distinctresponsibilities.

AngularJS does not implement MVC in the traditional sense, but rathersomething closer to MVVM (Model-View-ViewModel). The Angular JSteam refers it humorously as Model View Whatever.

9

Page 10: AngularJS

Core Features

Deep Linking:

Deep linking allows to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state.

Dependency Injection:

AngularJS has a built-in dependency injection subsystem that helps the developer to create, understand, and test the applications easily.

10

Page 11: AngularJS

Concepts

11

Page 12: AngularJS

Environment Setup

How to set up AngularJS library to be used in web application development

https://angularjs.org/

Downloading and hosting files locally

CDN access

12

Page 13: AngularJS

Environment Setup

CDN access:

The CDN gives you access to regional data centers. In this case, the Google host.

The CDN transfers the responsibility of hosting files from your own servers to a series of external ones.

It also offers an advantage that if the visitor of your web page has already downloaded a copy of AngularJS from the same CDN, there is no need to re-download it.

13

Page 14: AngularJS

Environment Setup

<head>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">

</script>

</head>

14

Page 15: AngularJS

MVC : Model View Controller

A software design pattern for developing web applications.

Model - It is the lowest level of the pattern responsible for maintaining data.

View - It is responsible for displaying all or a portion of the data to the user.

Controller - It is a software Code that controls the interactions between the Model and View.

15

Page 16: AngularJS

MVC : Model View Controller

View

ControllerModel

1. Event or User Action or View Load

2. Maps to particular Model after fetching the data

3. Implement the Business Logic on response data and Bind it to View

View : • Renders the Model data• Send User actions/events to controller• UI

Controller: • Define Application Behavior• Maps user actions to Model• Select view for response

Model: • Business Logic• Notify view changes• Application Functionality• Data in general

16

Page 17: AngularJS

MVC : Model View Controller

17

Page 18: AngularJS

Parts Of An AngularJS Application

ng-app : This directive defines and links an AngularJS application to HTML.

ng-model : This directive binds the values of AngularJS application data to HTML input controls.

ng-bind : This directive binds the AngularJS Application data to HTML tags.

18

Page 19: AngularJS

Creating AngularJS Application

Step 1: Load framework

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">

</script>

19

Page 20: AngularJS

Creating AngularJS Application

Step 2: Define AngularJS application using ng-app directive.

<div ng-app="">. . .

</div>

20

Page 21: AngularJS

Creating AngularJS Application

Step 3: Define a model name using ng-model directive.

<p>Enter Your Name: <input type="text" ng-model="name"></p>

21

Page 22: AngularJS

Creating AngularJS Application

Step 4: Bind the value of above model defined using ng-bind directive.

<p>Hello <span ng-bind="name"></span>!</p>

22

Page 23: AngularJS

Executing AngularJS Application

<html>

<title>AngularJS First Application</title>

<body>

<h1>Sample Application</h1>

<div ng-app="">

<p>Enter your Name:

<input type="text" ng-model="name">

</p>

<p>Hello <span ng-bind="name"></span>!</p>

</div>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

</body>

</html>

23

Page 24: AngularJS

How AngularJS Integrates with HTML

The ng-app directive indicates the start of AngularJS application.

The ng-model directive creates a model variable named name, which can be used with the HTML page and within the div having ng-app directive.

The ng-bind then uses the name model to be displayed in the HTML tag whenever user enters input in the text box.

Closing</div> tag indicates the end of AngularJS application.

24

Page 25: AngularJS

AngularJS Directives

AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix.

ng-app - This directive starts an AngularJS Application.

ng-init - This directive initializes application data.

ng-model - This directive defines the model that is variable to be used in AngularJS.

ng-repeat - This directive repeats HTML elements for each item in a collection.

25

Page 26: AngularJS

AngularJS Directives

<div ng-app="" ng-init="firstName='John'">

<p>Name: <input type="text" ng-model="firstName"></p>

<p>Hai <span ng-bind="firstName"> </span>!</p>

</div>

26

Page 27: AngularJS

AngularJS Directives

<div ng-app="" ng-init="names=['Jani','Hege','Kai']">

<ul>

<li ng-repeat="x in names">

{{ x }}

</li>

</ul>

</div>

27

Page 28: AngularJS

AngularJS Expressions

AngularJS binds data to HTML using Expressions.

AngularJS expressions are written inside double braces: {{ expression }}.

AngularJS expressions binds data to HTML the same way as the ng-bind directive.

AngularJS will "output" data exactly where the expression is written.

AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.

Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}

28

Page 29: AngularJS

AngularJS Expressions

<div ng-app="" ng-init="quantity=1;cost=5">

<p>Total in dollar: {{ quantity * cost }}</p>

</div>

29

Page 30: AngularJS

AngularJS Controllers

AngularJS applications are controlled by controllers.

The ng-controller directive defines the application controller.

A controller is a JavaScript Object, created by a standard JavaScript object constructor.

30

Page 31: AngularJS

AngularJS Controllers

<div ng-app="" ng-controller="personController">

First Name: <input type="text" ng-model="firstName"><br>Last Name: <input type="text" ng-model="lastName"><br>

Full Name: {{firstName + " " + lastName}}

</div>

<script>function personController($scope) {

$scope.firstName="John",$scope.lastName="Doe"

}</script>

31

Page 32: AngularJS

AngularJS Filters

32

Page 33: AngularJS

AngularJS Filters

33

<div ng-app="" ng-controller="personController">

<p>The name is {{ lastName | uppercase }}</p>

</div>

Page 34: AngularJS

AngularJS $http

AngularJS $http is a core service for reading data from web servers.

$http.get(url) is the function to use for reading server data.

34

Page 35: AngularJS

AngularJS $http

<div ng-app="" ng-controller="customersController">

<ul><li ng-repeat="x in names">

{{ x.Name + ', ' + x.Country }}</li>

</ul>

</div>

<script>function customersController($scope,$http) {

$http.get("http://www.w3schools.com/website/Customers_JSON.php")

.success(function(response) {$scope.names = response;});}</script>

35

Page 36: AngularJS

AngularJS Tables

Table data is generally repeatable.

The ng-repeat directive can be used to draw table easily.

36

Page 37: AngularJS

AngularJS Tables

<div ng-app="" ng-controller="customersController">

<table><tr ng-repeat="x in names">

<td>{{ x.Name }}</td><td>{{ x.Country }}</td>

</tr></table>

</div>

<script>function customersController($scope,$http) {

$http.get("http://www.w3schools.com/website/Customers_JSON.php").success(function(response) {$scope.names = response;});

}</script>

37

Page 38: AngularJS

AngularJS SQL

Fetching Data From a PHP Server Running MySQL

Fetching Data From an ASP.NET Server Running SQL

38

Page 39: AngularJS

AngularJS HTML DOM

39

Page 40: AngularJS

AngularJS HTML DOM

40

<div ng-app="">

<p><button ng-disabled="mySwitch">Click Me!</button></p>

<p><input type="checkbox" ng-model="mySwitch">Button</p>

</div>

Page 41: AngularJS

AngularJS Modules

Modules define applications. All application controllers should belong to a module.

Modules make your application more readable, and keep the global namespace clean.

41

Page 42: AngularJS

AngularJS Modules

<div ng-app="myApp" ng-controller="myCtrl">{{ firstName + " " + lastName }}</div>

<script>var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {$scope.firstName = "John";$scope.lastName = "Doe";

});</script>

42

Page 43: AngularJS

AngularJS Forms

Events

ng-click ng-dbl-click ng-mousedown

ng-mouseup ng-mouseenter ng-mouseleave

ng-mousemove ng-mouseover ng-keydown

ng-keyup ng-keypress ng-change

43

Page 44: AngularJS

AngularJS Input Validation

44

Property Description

$pristine The user has not interacted with the field yet.

$dirty The user has interacted with the field.

$valid The field content is valid.

$invalid The field content is invalid.

$error The exact error.

Page 45: AngularJS

AngularJS Includes

With AngularJS, you can include HTML files in HTML.

HTML Includes in Future HTML<link rel="import" href="/path/navigation.html">

Server Side Includes<?php require("navigation.php"); ?>

Client Side Includeshttp request (AJAX)

AngularJS Side Includesng-include

45

Page 46: AngularJS

AngularJS Includes

<body>

<div class="container"><div ng-include="'myUsers_List.htm'"></div><div ng-include="'myUsers_Form.htm'"></div>

</div>

</body>

46

Page 47: AngularJS

Advantages of AngularJS

It provides the capability to create Single Page Application in a very clean and maintainable way.

It provides data binding capability to HTML. Thus, it gives user a rich and responsive experience.

AngularJS code is unit testable.

AngularJS provides reusable components.

With AngularJS, the developers can achieve more functionality with short code.

In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing.

47

Page 48: AngularJS

Disadvantages of AngularJS

Not secure :

Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and authorization is must to keep an application secure.

Not degradable:

If the user of your application disables JavaScript, then nothing would be visible, except the basic page.

48

Page 49: AngularJS

49

Page 50: AngularJS

50