Introduction to AngularJS

Marco Vito Moscaritolo
Introduction to

HTML enhanced for web apps!
by Marco Vito Moscaritolo / @mavimo
<!-­-­ directive: ng-­mavimo -­-­>
{ { me.name } } -> Marco Vito Moscaritolo
{ { me.role } } -> Developer
{ { me.company.name | link } } -> Agavee
{ { me.twitter | link } } -> @mavimo
What's AngularJS
AngularJS is a toolset for building the framework most suited to
your application development.
Heads Up
Starting from directives...
...adding controllers...
...and filters...
...using routing...
...manage resources...
...and fun :)
Directives
Directives are a way to teach HTML new tricks. During DOM
compilation directives are matched against the HTML and
executed. This allows directives to register behavior, or
transform the DOM.
<span ng-repeat="exp"></span>
<span class="ng-repeat: exp;"></span>
<ng-repeat></ng-repeat>
<!-- directive: ng-repeat exp -->

The directives can be placed in element names, attributes,
class names, as well as comments.
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"
</head>
<body>
<div>
<input type="text" ng-model="sample" placeholder="Enter text">
<hr>
<h1>{ { sample } }!</h1>
</div>
</body>
</html>
Write here...
<!doctype html>
<html ng-app>
<head>
<script src="js/angular.min.js" ></script>
<script src="js/demo.js" ></script>
</head>
<body>
<ul ng-controller="getGdgList">
<li ng-repeat="gdg in gdgs">{ {gdg.name} } have { {gdg.members} } members
</ul>
</body>
</html>
GDG Milan have 92 members
GDG Barcelona have 228 members
GDG Silicon Valley have 171 members
GDG Stockholm have 119 members
GDG Herzeliya have 140 members
GDG Ahmedabad have 78 members
GDG Lagos have 115 members
GDG Bangalore have 109 members
GDG Lima have 175 members
GDG L-Ab have 77 members
and also
ng-show
ng-switch
ng-class
...
Controllers
<!doctype html>
<html ng-app>
<head>
<script src="js/angular.min.js" ></script>
<script src="js/demo.js" ></script>
</head>
<body>
<ul ng-controller="getTweets">
<li ng-repeat="tweet in tweets"></li>
</ul>
</body>
</html>
function getTweets($scope, $http) {
var search = 'gdg';
var url = 'http://search.twitter.com/search.json?q=' + search;
$http({ method: 'GET', url: url })
.success(function(data, status, headers, config) {
$scope.tweets = data.results;
})
.error(function(data, status, headers, config) {
console.log('Error: fetch tweets');
$scope.tweets = {};
});
return $scope;
}
Methods in controller
function getTweets($scope, $http) {
// ...
$scope.getMore = function () {
// ...
}
// ...
return $scope;
}]);
<ul ng-controller="getTweets">
<li ng-repeat="tweet in tweets"></li>
<li ng-click="getMore()">Get more</li>
</ul>
Controller in module
var gdgApp = angular.module('gdgApp', []);
gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) {
var search = 'gdg';
var url = 'http://search.twitter.com/search.json?q=' + search;
$http({ method: 'GET', url: url }).
success(function(data, status, headers, config) {
$scope.tweets = data.results;
}).
error(function(data, status, headers, config) {
console.log('Error: fetch tweets');
$scope.tweets = {};
});
return $scope;
}]);
Dependency Injection
(also know as Inversion of Control)
Dependency Injection (DI) is a software design pattern that
deals with how code gets hold of its dependencies.
Services
Angular services are singletons that carry out specific tasks.
Eg. $http service that provides low level access to the
browser's XMLHttpRequest object.
gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) {
// ...
return $scope;
}]);
angular.module('gdgModuleAlarm', []).
factory('alarm', function($window) {
return {
alarm: function(text) {
$window.alert(text);
}
};
}
);
Injectors
// New injector created from the previus declared module.
var injector = angular.injector(['gdgModuleAlarm', 'ng']);
// Request any dependency from the injector
var a = injector.get('alarm');
// We can also force injecting using
var alarmFactory = function (my$window) {};
alarmFactory.$inject = ['$window'];
Filters
Filters perform data transformation.
They follow the spirit of UNIX filters and use similar syntax |
(pipe).
<!doctype html>
<html ng-app>
<head>
<script src="js/angular.min.js" ></script>
<script src="js/demo.js" ></script>
</head>
<body>
<a href="" ng-click="predicate = 'members'; reverse=!reverse"<Sort</a>
<ul ng-controller="getGdgList">
<li ng-repeat="gdg in gdgs | orderBy:predicate:reverse "> have members
</ul>
</body>
</html>
Sort

GDG MILAN have 92 members
GDG BARCELONA have 228 members
GDG SILICON VALLEY have 171 members
GDG STOCKHOLM have 119 members
GDG HERZELIYA have 140 members
GDG AHMEDABAD have 78 members
GDG LAGOS have 115 members
GDG BANGALORE have 109 members
GDG LIMA have 175 members
GDG L-AB have 77 members
Creating custom filters
angular.module('agaveeApp', [])
.filter('orderByVersion', function() {
return function(modules, version) {
var parseVersionString = function (str) { /* .. */ };
var vMinMet = function(vmin, vcurrent) { /* .. */ };
var result = [];
for (var i = 0; i < modules.length; i++) {
if (vMinMet(modules[i].version_added, version)) {
result[result.length] = modules[i];
}
}
return result;
};
});
Model
The model is the data which is merged
with the template to produce the view.
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"
</head>
<body>
<div>
<input type="text" ng-model="sample" placeholder="Enter text">
<hr>
<h1>!</h1>
</div>
</body>
</html>
Configuration
angular.module('gdgApp', [])
.constant('gdg', {
'url' : 'http://localhost:3000',
'token': 'e9adf82fd1cb716548ef1d4621a5935dcf869817'
})
// Configure $http
.config(['$httpProvider', 'gdg',
function ($httpProvider, gdg) {
$httpProvider.defaults.headers.common['X-gdg-API-Key'] = gdg.token;
}
]);
Routing
angular.module('gdgApp', [])
// Configure services
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/projects', {
templateUrl: 'views/projects.html',
controller: 'ProjectsCtrl'
});
$routeProvider.when('/projects/:project', {
templateUrl: 'views/project.html',
controller: 'ProjectCtrl'
});
$routeProvider.otherwise({redirectTo: '/projects'});
}]);
Resource
angular.module('resources.project', ['ngResource'])
.factory('Project', ['$http', '$resource', 'gdg', function ($http, gdg) {
return $resource(gdg.url + '/project/:projectId', {projectId:'@id'}, {
query : { method : 'GET', isArray:true},
create : { method : 'POST' },
save
: { method : 'PUT' },
delete : { method : 'DELETE' }
});
}]);
// ...
var p = new Project();
p.name = 'GDG Milan';
p.$save();
angular.module('resources.project', ['ngResource'])
.factory('Project', ['$http', 'gdg', function ($http, gdg) {
var Project = function (data) {
angular.extend(this, data);
};
// a static method to retrieve Project by ID
Project.get = function (id) {
return $http.get(gdg.url + '/projects/' + id).then(function (response) {
return new Project(response.data);
});
};
// an instance method to create a new Project
Project.prototype.create = function () {
var project = this;
return $http.post(gdg.url + '/projects.json', project).then(function (response)
project.id = response.data.id;
Testing
Karma - a test runner that fits all our needs.
Jasmine - Jasmine is a behavior-driven development
framework for testing JavaScript code.
describe('Controller: getGdgList', function () {
// load the controller's module
beforeEach(module('gdgApp'));
var getGdgList, scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller) {
scope = {};
getGdgList = $controller('getGdgList', {
$scope: scope
});
}));
it('GDG List must display defined number of items', function () {
expect(scope.gdgs.length).toBe(10);
});
Angular 1.2
...is coming!
More modular
Introduce ng-animate (and related)
Support to mobile (÷/-)
Demo
?
Questions time
THE END

Marco Vito Moscaritolo / @mavimo
1 of 34

Recommended

AngularJS Framework by
AngularJS FrameworkAngularJS Framework
AngularJS FrameworkBarcamp Saigon
977 views29 slides
Blockly by
BlocklyBlockly
BlocklyJuliaDrozd
975 views13 slides
AngularJS Project Setup step-by- step guide - RapidValue Solutions by
AngularJS Project Setup step-by- step guide - RapidValue SolutionsAngularJS Project Setup step-by- step guide - RapidValue Solutions
AngularJS Project Setup step-by- step guide - RapidValue SolutionsRapidValue
13.1K views15 slides
Gae by
GaeGae
Gaeguest0e51364
600 views18 slides
AngularJs by
AngularJsAngularJs
AngularJssyam kumar kk
1K views19 slides
AngularJS Architecture by
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
64.9K views38 slides

More Related Content

What's hot

Odoo - CMS performances optimization by
Odoo - CMS performances optimizationOdoo - CMS performances optimization
Odoo - CMS performances optimizationOdoo
5.3K views16 slides
Advanced Tips & Tricks for using Angular JS by
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSSimon Guest
19.4K views70 slides
A gently introduction to AngularJS by
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJSGregor Woiwode
1.2K views33 slides
Angular JS blog tutorial by
Angular JS blog tutorialAngular JS blog tutorial
Angular JS blog tutorialClaude Tech
4.3K views43 slides
Private slideshow by
Private slideshowPrivate slideshow
Private slideshowsblackman
878 views16 slides
Angular js by
Angular jsAngular js
Angular jsBehind D Walls
2K views34 slides

What's hot(20)

Odoo - CMS performances optimization by Odoo
Odoo - CMS performances optimizationOdoo - CMS performances optimization
Odoo - CMS performances optimization
Odoo5.3K views
Advanced Tips & Tricks for using Angular JS by Simon Guest
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
Simon Guest19.4K views
A gently introduction to AngularJS by Gregor Woiwode
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJS
Gregor Woiwode1.2K views
Angular JS blog tutorial by Claude Tech
Angular JS blog tutorialAngular JS blog tutorial
Angular JS blog tutorial
Claude Tech4.3K views
Private slideshow by sblackman
Private slideshowPrivate slideshow
Private slideshow
sblackman878 views
[FEConf Korea 2017]Angular 컴포넌트 대화법 by Jeado Ko
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법
Jeado Ko2.4K views
Enjoy the vue.js by TechExeter
Enjoy the vue.jsEnjoy the vue.js
Enjoy the vue.js
TechExeter1.4K views
[AngularJS] From Angular to Mobile in 30 minutes by Globant
[AngularJS] From Angular to Mobile in 30 minutes[AngularJS] From Angular to Mobile in 30 minutes
[AngularJS] From Angular to Mobile in 30 minutes
Globant1.4K views
Building a Startup Stack with AngularJS by FITC
Building a Startup Stack with AngularJSBuilding a Startup Stack with AngularJS
Building a Startup Stack with AngularJS
FITC32K views
AngularJS in 60ish Minutes by Dan Wahlin
AngularJS in 60ish MinutesAngularJS in 60ish Minutes
AngularJS in 60ish Minutes
Dan Wahlin16.5K views
Aleksey Bogachuk - "Offline Second" by IT Event
Aleksey Bogachuk - "Offline Second"Aleksey Bogachuk - "Offline Second"
Aleksey Bogachuk - "Offline Second"
IT Event230 views
The Magic of Advanced Debugging by Ivelina Dimova
The Magic of Advanced DebuggingThe Magic of Advanced Debugging
The Magic of Advanced Debugging
Ivelina Dimova142 views

Viewers also liked

Managing Quality in the Front End World by
Managing Quality in the Front End WorldManaging Quality in the Front End World
Managing Quality in the Front End WorldMarco Vito Moscaritolo
892 views24 slides
Modulo drupal dati_geofisici_applicazioni_scientifiche by
Modulo drupal dati_geofisici_applicazioni_scientificheModulo drupal dati_geofisici_applicazioni_scientifiche
Modulo drupal dati_geofisici_applicazioni_scientificheAlma mater studiorum - Università di Bologna
326 views20 slides
Your Entity, Your Code by
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your CodeMarco Vito Moscaritolo
770 views36 slides
Drupal7 by
Drupal7Drupal7
Drupal7Marco Vito Moscaritolo
655 views21 slides
Introduction to angular js by
Introduction to angular jsIntroduction to angular js
Introduction to angular jsMarco Vito Moscaritolo
1.1K views32 slides
Front End Optimization, 'The Cloud' can help you! by
Front End Optimization, 'The Cloud' can help you!Front End Optimization, 'The Cloud' can help you!
Front End Optimization, 'The Cloud' can help you!Marco Vito Moscaritolo
1.4K views36 slides

Similar to Introduction to AngularJS

AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS by
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSmurtazahaveliwala
777 views28 slides
AngularJS 101 - Everything you need to know to get started by
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
55.5K views41 slides
Angular JS deep dive by
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
1.5K views15 slides
Grails Advanced by
Grails Advanced Grails Advanced
Grails Advanced Saurabh Dixit
3.6K views75 slides
AngularJS Mobile Warsaw 20-10-2014 by
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014Dariusz Kalbarczyk
709 views29 slides
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios by
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosLearnimtactics
3.9K views21 slides

Similar to Introduction to AngularJS(20)

AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS by murtazahaveliwala
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
murtazahaveliwala777 views
AngularJS 101 - Everything you need to know to get started by Stéphane Bégaudeau
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
Stéphane Bégaudeau55.5K views
Angular JS deep dive by Axilis
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
Axilis1.5K views
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios by Learnimtactics
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
Learnimtactics 3.9K views
Angular Js Get Started - Complete Course by EPAM Systems
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
EPAM Systems3.1K views
AngularJs Workshop SDP December 28th 2014 by Ran Wahle
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
Ran Wahle510 views
DevFest Chiang Mai - Implementing Google Analytics - 2011-09-24.ppt by Vinoaj Vijeyakumaar
DevFest Chiang Mai - Implementing Google Analytics - 2011-09-24.pptDevFest Chiang Mai - Implementing Google Analytics - 2011-09-24.ppt
DevFest Chiang Mai - Implementing Google Analytics - 2011-09-24.ppt
Vinoaj Vijeyakumaar2.1K views
Pengenalan AngularJS by Edi Santoso
Pengenalan AngularJSPengenalan AngularJS
Pengenalan AngularJS
Edi Santoso405 views
Different way to share data between controllers in angular js by codeandyou forums
Different way to share data between controllers in angular jsDifferent way to share data between controllers in angular js
Different way to share data between controllers in angular js
codeandyou forums4.3K views
Sharing Data between controllers in different ways. by Amar Shukla
Sharing Data between controllers in different ways.Sharing Data between controllers in different ways.
Sharing Data between controllers in different ways.
Amar Shukla71 views

Recently uploaded

Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...ShapeBlue
180 views18 slides
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates by
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesKeynote Talk: Open Source is Not Dead - Charles Schulz - Vates
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesShapeBlue
252 views15 slides
Kyo - Functional Scala 2023.pdf by
Kyo - Functional Scala 2023.pdfKyo - Functional Scala 2023.pdf
Kyo - Functional Scala 2023.pdfFlavio W. Brasil
457 views92 slides
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITShapeBlue
206 views8 slides
The Power of Heat Decarbonisation Plans in the Built Environment by
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built EnvironmentIES VE
79 views20 slides
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...ShapeBlue
139 views29 slides

Recently uploaded(20)

Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue180 views
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates by ShapeBlue
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesKeynote Talk: Open Source is Not Dead - Charles Schulz - Vates
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates
ShapeBlue252 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue206 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE79 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue139 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue119 views
Business Analyst Series 2023 - Week 4 Session 8 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 8Business Analyst Series 2023 -  Week 4 Session 8
Business Analyst Series 2023 - Week 4 Session 8
DianaGray10123 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue263 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue159 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10139 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue180 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue138 views
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by ShapeBlue
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
ShapeBlue184 views
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ... by ShapeBlue
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
How to Re-use Old Hardware with CloudStack. Saving Money and the Environment ...
ShapeBlue166 views
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue152 views
Confidence in CloudStack - Aron Wagner, Nathan Gleason - Americ by ShapeBlue
Confidence in CloudStack - Aron Wagner, Nathan Gleason - AmericConfidence in CloudStack - Aron Wagner, Nathan Gleason - Americ
Confidence in CloudStack - Aron Wagner, Nathan Gleason - Americ
ShapeBlue130 views
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue147 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue203 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue161 views

Introduction to AngularJS

  • 1. Introduction to HTML enhanced for web apps! by Marco Vito Moscaritolo / @mavimo
  • 2. <!-­-­ directive: ng-­mavimo -­-­> { { me.name } } -> Marco Vito Moscaritolo { { me.role } } -> Developer { { me.company.name | link } } -> Agavee { { me.twitter | link } } -> @mavimo
  • 3. What's AngularJS AngularJS is a toolset for building the framework most suited to your application development.
  • 4. Heads Up Starting from directives... ...adding controllers... ...and filters... ...using routing... ...manage resources... ...and fun :)
  • 5. Directives Directives are a way to teach HTML new tricks. During DOM compilation directives are matched against the HTML and executed. This allows directives to register behavior, or transform the DOM. <span ng-repeat="exp"></span> <span class="ng-repeat: exp;"></span> <ng-repeat></ng-repeat> <!-- directive: ng-repeat exp --> The directives can be placed in element names, attributes, class names, as well as comments.
  • 6. <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js" </head> <body> <div> <input type="text" ng-model="sample" placeholder="Enter text"> <hr> <h1>{ { sample } }!</h1> </div> </body> </html>
  • 8. <!doctype html> <html ng-app> <head> <script src="js/angular.min.js" ></script> <script src="js/demo.js" ></script> </head> <body> <ul ng-controller="getGdgList"> <li ng-repeat="gdg in gdgs">{ {gdg.name} } have { {gdg.members} } members </ul> </body> </html>
  • 9. GDG Milan have 92 members GDG Barcelona have 228 members GDG Silicon Valley have 171 members GDG Stockholm have 119 members GDG Herzeliya have 140 members GDG Ahmedabad have 78 members GDG Lagos have 115 members GDG Bangalore have 109 members GDG Lima have 175 members GDG L-Ab have 77 members
  • 11. Controllers <!doctype html> <html ng-app> <head> <script src="js/angular.min.js" ></script> <script src="js/demo.js" ></script> </head> <body> <ul ng-controller="getTweets"> <li ng-repeat="tweet in tweets"></li> </ul> </body> </html>
  • 12. function getTweets($scope, $http) { var search = 'gdg'; var url = 'http://search.twitter.com/search.json?q=' + search; $http({ method: 'GET', url: url }) .success(function(data, status, headers, config) { $scope.tweets = data.results; }) .error(function(data, status, headers, config) { console.log('Error: fetch tweets'); $scope.tweets = {}; }); return $scope; }
  • 13. Methods in controller function getTweets($scope, $http) { // ... $scope.getMore = function () { // ... } // ... return $scope; }]); <ul ng-controller="getTweets"> <li ng-repeat="tweet in tweets"></li> <li ng-click="getMore()">Get more</li> </ul>
  • 14. Controller in module var gdgApp = angular.module('gdgApp', []); gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) { var search = 'gdg'; var url = 'http://search.twitter.com/search.json?q=' + search; $http({ method: 'GET', url: url }). success(function(data, status, headers, config) { $scope.tweets = data.results; }). error(function(data, status, headers, config) { console.log('Error: fetch tweets'); $scope.tweets = {}; }); return $scope; }]);
  • 15. Dependency Injection (also know as Inversion of Control) Dependency Injection (DI) is a software design pattern that deals with how code gets hold of its dependencies.
  • 16. Services Angular services are singletons that carry out specific tasks. Eg. $http service that provides low level access to the browser's XMLHttpRequest object.
  • 17. gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) { // ... return $scope; }]); angular.module('gdgModuleAlarm', []). factory('alarm', function($window) { return { alarm: function(text) { $window.alert(text); } }; } );
  • 18. Injectors // New injector created from the previus declared module. var injector = angular.injector(['gdgModuleAlarm', 'ng']); // Request any dependency from the injector var a = injector.get('alarm'); // We can also force injecting using var alarmFactory = function (my$window) {}; alarmFactory.$inject = ['$window'];
  • 19. Filters Filters perform data transformation. They follow the spirit of UNIX filters and use similar syntax | (pipe).
  • 20. <!doctype html> <html ng-app> <head> <script src="js/angular.min.js" ></script> <script src="js/demo.js" ></script> </head> <body> <a href="" ng-click="predicate = 'members'; reverse=!reverse"<Sort</a> <ul ng-controller="getGdgList"> <li ng-repeat="gdg in gdgs | orderBy:predicate:reverse "> have members </ul> </body> </html>
  • 21. Sort GDG MILAN have 92 members GDG BARCELONA have 228 members GDG SILICON VALLEY have 171 members GDG STOCKHOLM have 119 members GDG HERZELIYA have 140 members GDG AHMEDABAD have 78 members GDG LAGOS have 115 members GDG BANGALORE have 109 members GDG LIMA have 175 members GDG L-AB have 77 members
  • 22. Creating custom filters angular.module('agaveeApp', []) .filter('orderByVersion', function() { return function(modules, version) { var parseVersionString = function (str) { /* .. */ }; var vMinMet = function(vmin, vcurrent) { /* .. */ }; var result = []; for (var i = 0; i < modules.length; i++) { if (vMinMet(modules[i].version_added, version)) { result[result.length] = modules[i]; } } return result; }; });
  • 23. Model The model is the data which is merged with the template to produce the view.
  • 24. <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js" </head> <body> <div> <input type="text" ng-model="sample" placeholder="Enter text"> <hr> <h1>!</h1> </div> </body> </html>
  • 25. Configuration angular.module('gdgApp', []) .constant('gdg', { 'url' : 'http://localhost:3000', 'token': 'e9adf82fd1cb716548ef1d4621a5935dcf869817' }) // Configure $http .config(['$httpProvider', 'gdg', function ($httpProvider, gdg) { $httpProvider.defaults.headers.common['X-gdg-API-Key'] = gdg.token; } ]);
  • 26. Routing angular.module('gdgApp', []) // Configure services .config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/projects', { templateUrl: 'views/projects.html', controller: 'ProjectsCtrl' }); $routeProvider.when('/projects/:project', { templateUrl: 'views/project.html', controller: 'ProjectCtrl' }); $routeProvider.otherwise({redirectTo: '/projects'}); }]);
  • 27. Resource angular.module('resources.project', ['ngResource']) .factory('Project', ['$http', '$resource', 'gdg', function ($http, gdg) { return $resource(gdg.url + '/project/:projectId', {projectId:'@id'}, { query : { method : 'GET', isArray:true}, create : { method : 'POST' }, save : { method : 'PUT' }, delete : { method : 'DELETE' } }); }]); // ... var p = new Project(); p.name = 'GDG Milan'; p.$save();
  • 28. angular.module('resources.project', ['ngResource']) .factory('Project', ['$http', 'gdg', function ($http, gdg) { var Project = function (data) { angular.extend(this, data); }; // a static method to retrieve Project by ID Project.get = function (id) { return $http.get(gdg.url + '/projects/' + id).then(function (response) { return new Project(response.data); }); }; // an instance method to create a new Project Project.prototype.create = function () { var project = this; return $http.post(gdg.url + '/projects.json', project).then(function (response) project.id = response.data.id;
  • 29. Testing Karma - a test runner that fits all our needs. Jasmine - Jasmine is a behavior-driven development framework for testing JavaScript code.
  • 30. describe('Controller: getGdgList', function () { // load the controller's module beforeEach(module('gdgApp')); var getGdgList, scope; // Initialize the controller and a mock scope beforeEach(inject(function ($controller) { scope = {}; getGdgList = $controller('getGdgList', { $scope: scope }); })); it('GDG List must display defined number of items', function () { expect(scope.gdgs.length).toBe(10); });
  • 31. Angular 1.2 ...is coming! More modular Introduce ng-animate (and related) Support to mobile (÷/-)
  • 32. Demo
  • 34. THE END Marco Vito Moscaritolo / @mavimo