KEMBAR78
Angular js | PPTX
AngularJS
QUYETVV
A story…
- SPA with todoapp
- jQuery
- knockout: Binding
Angular là gì?
• AngularJS is a Javascript MVC framework created by Google to build properly architectured
and maintenable web applications.
• It’s not a JavaScript library (As they say). There are no functions which we can directly call and
use.
• It is not a DOM manipulation library like jQuery. But it uses subset of jQuery for DOM
manipulation (called jqLite).
• Focus more on HTML side of web apps.
• For MVC/MVVM design pattern
Angular app models
Philosophy
”ANGULARJS is built around the philosophy that declarative code is better than imperative code
while building UIs and wiring different components of web application together.”
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
Key Features of ANGULARJS
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 that are bound to a particular scope.
Services − AngularJS come with several built-in services for example $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 (ngBind, ngModel...)
Templates − These are the rendered view with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using
"partials".
Routing − It is concept of switching views.
Model View Whatever − MVC is a design pattern for dividing an application into different parts (called Model, View and Controller), each with distinct responsibilities.
AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular JS team refers it humorously as
Model View Whatever.
Deep Linking − Deep linking allows you 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 by making the application easier to develop, understand, and test.
Concepts
Advantages of AngularJS
AngularJS provides capability to create Single Page Application in a very clean and maintainable
way.
AngularJS provides data binding capability to HTML thus giving user a rich and responsive
experience
AngularJS code is unit testable.
AngularJS uses dependency injection and make use of separation of concerns.
AngularJS provides reusable components.
With AngularJS, developer write less code and get more functionality.
In AngularJS, views are pure html pages, and controllers written in JavaScript do the business
processing.
AngularJS applications can run on all major browsers and smart phones including Android and
iOS based phones/tablets.
Disadvantages of AngularJS
Though AngularJS comes with lots of plus points but same time we should consider the
following points −
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 your application user disables JavaScript then user will just see the basic
page and nothing more.
Where I can use it?
Views, Controllers
and Scope
View Controller$scope
$scope is the "glue" (ViewModel)
between a controller and a view
View, Controllers and Scope
<div class="container" data-ng-controller="SimpleController as vm">
<h3>Adding a Simple Controller</h3>
<ul>
<li data-ng-repeat="cust in vm.customers">
{{ cust.name }} - {{ cust.city }}
</li>
</ul>
</div>
<script>
function SimpleController($scope) {
$scope.customers = [
{ name: 'Dave Jones', city: 'Phoenix' },
{ name: 'Jamie Riley', city: 'Atlanta' },
{ name: 'Heedy Wahlin', city: 'Chandler' },
{ name: 'Thomas Winter', city: 'Seattle' }
];
}
</script>
Define the
controller to use
Basic controller
$scope injected
dynamically
Access $scope
Creating a View and Controller
Further topic
Rootscope, scope hierarchize
Communicate between controller
ControllerAs
Demo
Directives, Filters
and Data Binding
What are Directives?
They teach HTML new tricks!
Using Directives and Data Binding
<!DOCTYPE html>
<html ng-app>
<head>
<title></title>
</head>
<body>
<div class="container">
Name: <input type="text" ng-model="name" /> {{ name }}
</div>
<script src="Scripts/angular.js"></script>
</body>
</html>
Directive
Directive
Data Binding Expression
<html data-ng-app="">
...
<div class="container"
data-ng-init="names=['Dave','Napur','Heedy','Shriva']">
<h3>Looping with the ng-repeat Directive</h3>
<ul>
<li data-ng-repeat="name in names">{{ name }}</li>
</ul>
</div>
...
</html>
Iterate through
names
Iterating with the ng-repeat Directive
AngularJS Help for Directives
<ul>
<li data-ng-repeat="cust in customers | orderBy:'name'">
{{ cust.name | uppercase }}
</li>
</ul> Order customers
by name property
<input type="text" data-ng-model="nameText" />
<ul>
<li data-ng-repeat="cust in customers | filter:nameText | orderBy:'name'">
{{ cust.name }} - {{ cust.city }}</li>
</ul>
Filter customers by
model value
Using Filters
AngularJS Help for Filters
Further topic
Directive detail options
Communication between container’s scope and directive scope
Nested scope and nested directive
Filter implementation
Demo
Modules, Routes
and Factories
View Controller
*FactoryDirectives
Routes
Module
Config
$scope
ControllerFactoryDirective
Routes
Module
Config
Service
Provider
<html ng-app="moduleName">
Modules are Containers
Value
Filter
var demoApp = angular.module('demoApp', []);
What's the Array for?
var demoApp = angular.module('demoApp',
['helperModule']);
Module that demoApp
depends on
Creating a Module
var demoApp = angular.module('demoApp', []);
demoApp.controller('SimpleController', function ($scope) {
$scope.customers = [
{ name: 'Dave Jones', city: 'Phoenix' },
{ name: 'Jamie Riley', city: 'Atlanta' },
{ name: 'Heedy Wahlin', city: 'Chandler' },
{ name: 'Thomas Winter', city: 'Seattle' }
];
});
Define a Module
Define a Controller
Creating a Controller in a Module
The Role of Routes
SPA Demo
http://www.myspa.com
View1 View2
View4 View3
/view2
/view3
/view4
/view1
Defining Routes
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(function ($routeProvider) {
$routeProvider
.when('/',
{
controller: 'SimpleController',
templateUrl:'View1.html'
})
.when('/view2',
{
controller: 'Simple2Controller',
templateUrl:'View2.html'
})
.otherwise({ redirectTo: '/' });
});
Define Module
Routes
SPA Demo
http://www.myspa.com
Where do Views Go in a Page?
Dynamically loaded views are injected into the shell
page as a module loads:
<div ng-view></div>
<ng-view></ng-view>
OR
View1
The Role of Factories
var demoApp = angular.module('demoApp', [])
.factory('simpleFactory', function () {
var factory = {};
var customers = [ ... ];
factory.getCustomers = function () {
return customers;
};
return factory;
})
.controller('SimpleController', function ($scope,
simpleFactory) {
$scope.customers = simpleFactory.getCustomers();
});
Factory injected into
controller at runtime
View Controller
*FactoryDirectives
Routes
Module
Config
$scope
Further topic
Provider
Injector
$q/promise
Exception handler
Validation
Cache/Storage
Bower
Gulp
Interceptor
Unit test
Pattern and code guide
Follow
http://bguiz.github.io/js-standards/angularjs/directives/
References
https://github.com/airbnb/javascript#objects
http://jscs.info/overview
https://github.com/angular/angular.js/wiki/Best-Practices
Demo
Our job
Fiin Pro Web
Demo current stage of FiinPro.Web
http://localhost:16550/index.html
Angular js

Angular js

  • 1.
  • 3.
    A story… - SPAwith todoapp - jQuery - knockout: Binding
  • 4.
    Angular là gì? •AngularJS is a Javascript MVC framework created by Google to build properly architectured and maintenable web applications. • It’s not a JavaScript library (As they say). There are no functions which we can directly call and use. • It is not a DOM manipulation library like jQuery. But it uses subset of jQuery for DOM manipulation (called jqLite). • Focus more on HTML side of web apps. • For MVC/MVVM design pattern
  • 5.
  • 6.
    Philosophy ”ANGULARJS is builtaround the philosophy that declarative code is better than imperative code while building UIs and wiring different components of web application together.” <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> </head> <body> <div> <label>Name:</label> <input type="text" ng-model="yourName" placeholder="Enter a name here"> <hr> <h1>Hello {{yourName}}!</h1> </div> </body> </html>
  • 7.
    Key Features ofANGULARJS 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 that are bound to a particular scope. Services − AngularJS come with several built-in services for example $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 (ngBind, ngModel...) Templates − These are the rendered view with information from the controller and model. These can be a single file (like index.html) or multiple views in one page using "partials". Routing − It is concept of switching views. Model View Whatever − MVC is a design pattern for dividing an application into different parts (called Model, View and Controller), each with distinct responsibilities. AngularJS does not implement MVC in the traditional sense, but rather something closer to MVVM (Model-View-ViewModel). The Angular JS team refers it humorously as Model View Whatever. Deep Linking − Deep linking allows you 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 by making the application easier to develop, understand, and test.
  • 8.
  • 9.
    Advantages of AngularJS AngularJSprovides capability to create Single Page Application in a very clean and maintainable way. AngularJS provides data binding capability to HTML thus giving user a rich and responsive experience AngularJS code is unit testable. AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. With AngularJS, developer write less code and get more functionality. In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing. AngularJS applications can run on all major browsers and smart phones including Android and iOS based phones/tablets.
  • 10.
    Disadvantages of AngularJS ThoughAngularJS comes with lots of plus points but same time we should consider the following points − 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 your application user disables JavaScript then user will just see the basic page and nothing more.
  • 11.
    Where I canuse it?
  • 12.
  • 13.
    View Controller$scope $scope isthe "glue" (ViewModel) between a controller and a view View, Controllers and Scope
  • 14.
    <div class="container" data-ng-controller="SimpleControlleras vm"> <h3>Adding a Simple Controller</h3> <ul> <li data-ng-repeat="cust in vm.customers"> {{ cust.name }} - {{ cust.city }} </li> </ul> </div> <script> function SimpleController($scope) { $scope.customers = [ { name: 'Dave Jones', city: 'Phoenix' }, { name: 'Jamie Riley', city: 'Atlanta' }, { name: 'Heedy Wahlin', city: 'Chandler' }, { name: 'Thomas Winter', city: 'Seattle' } ]; } </script> Define the controller to use Basic controller $scope injected dynamically Access $scope Creating a View and Controller
  • 15.
    Further topic Rootscope, scopehierarchize Communicate between controller ControllerAs
  • 16.
  • 17.
  • 18.
    What are Directives? Theyteach HTML new tricks!
  • 19.
    Using Directives andData Binding <!DOCTYPE html> <html ng-app> <head> <title></title> </head> <body> <div class="container"> Name: <input type="text" ng-model="name" /> {{ name }} </div> <script src="Scripts/angular.js"></script> </body> </html> Directive Directive Data Binding Expression
  • 20.
    <html data-ng-app=""> ... <div class="container" data-ng-init="names=['Dave','Napur','Heedy','Shriva']"> <h3>Loopingwith the ng-repeat Directive</h3> <ul> <li data-ng-repeat="name in names">{{ name }}</li> </ul> </div> ... </html> Iterate through names Iterating with the ng-repeat Directive
  • 21.
  • 22.
    <ul> <li data-ng-repeat="cust incustomers | orderBy:'name'"> {{ cust.name | uppercase }} </li> </ul> Order customers by name property <input type="text" data-ng-model="nameText" /> <ul> <li data-ng-repeat="cust in customers | filter:nameText | orderBy:'name'"> {{ cust.name }} - {{ cust.city }}</li> </ul> Filter customers by model value Using Filters
  • 23.
  • 24.
    Further topic Directive detailoptions Communication between container’s scope and directive scope Nested scope and nested directive Filter implementation
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
    var demoApp =angular.module('demoApp', []); What's the Array for? var demoApp = angular.module('demoApp', ['helperModule']); Module that demoApp depends on Creating a Module
  • 30.
    var demoApp =angular.module('demoApp', []); demoApp.controller('SimpleController', function ($scope) { $scope.customers = [ { name: 'Dave Jones', city: 'Phoenix' }, { name: 'Jamie Riley', city: 'Atlanta' }, { name: 'Heedy Wahlin', city: 'Chandler' }, { name: 'Thomas Winter', city: 'Seattle' } ]; }); Define a Module Define a Controller Creating a Controller in a Module
  • 31.
    The Role ofRoutes SPA Demo http://www.myspa.com View1 View2 View4 View3 /view2 /view3 /view4 /view1
  • 32.
    Defining Routes var demoApp= angular.module('demoApp', ['ngRoute']); demoApp.config(function ($routeProvider) { $routeProvider .when('/', { controller: 'SimpleController', templateUrl:'View1.html' }) .when('/view2', { controller: 'Simple2Controller', templateUrl:'View2.html' }) .otherwise({ redirectTo: '/' }); }); Define Module Routes
  • 33.
    SPA Demo http://www.myspa.com Where doViews Go in a Page? Dynamically loaded views are injected into the shell page as a module loads: <div ng-view></div> <ng-view></ng-view> OR View1
  • 34.
    The Role ofFactories var demoApp = angular.module('demoApp', []) .factory('simpleFactory', function () { var factory = {}; var customers = [ ... ]; factory.getCustomers = function () { return customers; }; return factory; }) .controller('SimpleController', function ($scope, simpleFactory) { $scope.customers = simpleFactory.getCustomers(); }); Factory injected into controller at runtime
  • 35.
  • 36.
  • 37.
    Pattern and codeguide Follow http://bguiz.github.io/js-standards/angularjs/directives/ References https://github.com/airbnb/javascript#objects http://jscs.info/overview https://github.com/angular/angular.js/wiki/Best-Practices
  • 38.
  • 39.
    Our job Fiin ProWeb Demo current stage of FiinPro.Web http://localhost:16550/index.html

Editor's Notes

  • #3 You are not in a hot trend of world It is enough stable to follow Many follower and large community
  • #4 http://todomvc.com/examples/angularjs/#/
  • #5 ViewModel: Lớp trung gian giữa View và Model. ViewModel có thể được xem là thành phần thay thế cho Controller trong mô hình MVC. Nó chứa các mã lệnh cần thiết để thực hiện data binding, command.  
  • #6 MVC in MVC
  • #7 Imperative programming A programming language that requires programming discipline such as C/C++, Java, COBOL, FORTRAN, Perl and JavaScript. Programmers writing in such languages must develop a proper order of actions in order to solve the problem, based on a knowledge of data processing and programming. Declarative programming A computer language that does not require writing traditional programming logic; Users concentrate on defining the input and output rather than the program steps required in a procedural programming language such as C++ or Java. Declarative programming examples are CSS, HTML, XML, XSLT, RegX.
  • #10 Good framework
  • #12 SPA Some pages with complicated layout/html
  • #17 http://plnkr.co/edit/C4R4xgEjnR0fQZuf3GcU?p=preview
  • #23 http://plnkr.co/edit/C4R4xgEjnR0fQZuf3GcU?p=preview
  • #26 http://plnkr.co/edit/3LJGxzlW4YJLpJHNh05G
  • #36 Provider
  • #39  Simple: http://plnkr.co/edit/9DtXX1pOQXGucqMzq9CW?p=preview More simple: https://jsfiddle.net/viralpatel/84gJX/?utm_source=website&utm_medium=embed&utm_campaign=84gJX