SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
INTRODUCTION TO
HTML enhanced for web apps!
by /Marco Vito Moscaritolo @mavimo
<!-- DIRECTIVE: NG-MAVIMO -->
{ { name } }-> Marco Vito Moscaritolo
{ { role } }-> Developer
{ { company | link } }->
{ { twitter | link } }->
Agavee
@mavimo
HEADS UP
Starting from template...
...adding controllers...
...and filters...
...using routing...
...manage resources...
...and fun :)
TEMPLATES (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.
The directives can be placed in element names, attributes,
class names, as well as comments.
<span ng-repeat="exp"></span>
<span class="ng-repeat: exp;"></span>
<ng-repeat></ng-repeat>
<!-- directive: ng-repeat exp -->
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angula
</head>
<body>
<div>
<input type="text" ng-model="sample" placeholder="Enter text">
<hr>
<h1>!</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"> have members</li>
</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
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>
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
Sort
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/angula
</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 (r
project.id = response.data.id;
return project;
});
};
// an instance method to pudate create a new Project
Project.prototype.update = function () {
var project = this;
return $http.put(gdg.url + '/projects/' + project.id + '.json', project
return project;
});
};
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);
});
it('GDG item must have members up to 0', function () {
angular.forEach(scope.gdgs,function (key, gdg) {
expect(gdg.members).toBeGreaterThan(0);
}
});
it('GDG item must have name and members', function () {
angular.forEach(scope.gdgs,function (key, gdg) {
expect(gdg.name).toBeDefined();
expect(gdg.members).toBeDefined();
?
Questions time
THE END
MARCO VITO MOSCARITOLO / @MAVIMO

Weitere ähnliche Inhalte

Was ist angesagt?

Modules and injector
Modules and injectorModules and injector
Modules and injectorEyal Vardi
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS ServicesEyal Vardi
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS RoutingEyal Vardi
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing optionsNir Kaufman
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Eyal Vardi
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDartLoc Nguyen
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xEyal Vardi
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0Eyal Vardi
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS AnimationsEyal Vardi
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2Eyal Vardi
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced RoutingAlexe Bogdan
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesEyal Vardi
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Eyal Vardi
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And NavigationEyal Vardi
 
Decoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDDecoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDAleix Vergés
 

Was ist angesagt? (20)

Modules and injector
Modules and injectorModules and injector
Modules and injector
 
AngularJS Services
AngularJS ServicesAngularJS Services
AngularJS Services
 
AngularJs
AngularJsAngularJs
AngularJs
 
AngularJS Routing
AngularJS RoutingAngularJS Routing
AngularJS Routing
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0Component lifecycle hooks in Angular 2.0
Component lifecycle hooks in Angular 2.0
 
Dart and AngularDart
Dart and AngularDartDart and AngularDart
Dart and AngularDart
 
Upgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.xUpgrading from Angular 1.x to Angular 2.x
Upgrading from Angular 1.x to Angular 2.x
 
Template syntax in Angular 2.0
Template syntax in Angular 2.0Template syntax in Angular 2.0
Template syntax in Angular 2.0
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS Animations
 
Performance Optimization In Angular 2
Performance Optimization In Angular 2Performance Optimization In Angular 2
Performance Optimization In Angular 2
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced Routing
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
AngularJS - $http & $resource Services
AngularJS - $http & $resource ServicesAngularJS - $http & $resource Services
AngularJS - $http & $resource Services
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture (Bucharest 26/10/2016)
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Routing And Navigation
Routing And NavigationRouting And Navigation
Routing And Navigation
 
Decoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDDecoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDD
 

Ähnlich wie Introduction to angular js

Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJSWei Ru
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript FrameworkAll Things Open
 
AngularJS 101 - Everything you need to know to get started
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
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar SimovićJS Belgrade
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 
Opencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJSOpencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJSbuttyx
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
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
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)Igor Bronovskyy
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014Dariusz Kalbarczyk
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routingjagriti srivastava
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IVisual Engineering
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
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
 

Ähnlich wie Introduction to angular js (20)

Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Choosing a Javascript Framework
Choosing a Javascript FrameworkChoosing a Javascript Framework
Choosing a Javascript Framework
 
AngularJS 101 - Everything you need to know to get started
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
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
AngularJS
AngularJSAngularJS
AngularJS
 
Opencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJSOpencast Admin UI - Introduction to developing using AngularJS
Opencast Admin UI - Introduction to developing using AngularJS
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
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
 
Built in filters
Built in filtersBuilt in filters
Built in filters
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
 
AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
 
Workshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte IWorkshop 12: AngularJS Parte I
Workshop 12: AngularJS Parte I
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
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
 

Mehr von Marco Vito Moscaritolo

Mehr von Marco Vito Moscaritolo (9)

Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Managing Quality in the Front End World
Managing Quality in the Front End WorldManaging Quality in the Front End World
Managing Quality in the Front End World
 
Build your own entity with Drupal
Build your own entity with DrupalBuild your own entity with Drupal
Build your own entity with Drupal
 
Front End Optimization, 'The Cloud' can help you!
Front End Optimization, 'The Cloud' can help you!Front End Optimization, 'The Cloud' can help you!
Front End Optimization, 'The Cloud' can help you!
 
Organizza il lavoro
Organizza il lavoroOrganizza il lavoro
Organizza il lavoro
 
HTML5, il lato client della forza...
HTML5, il lato client della forza... HTML5, il lato client della forza...
HTML5, il lato client della forza...
 
Drupal7
Drupal7Drupal7
Drupal7
 
Alpine Drupal Camp 2011 - Data migration
Alpine Drupal Camp 2011 - Data migrationAlpine Drupal Camp 2011 - Data migration
Alpine Drupal Camp 2011 - Data migration
 
Sviluppare estensioni per google chrome
Sviluppare estensioni per google chromeSviluppare estensioni per google chrome
Sviluppare estensioni per google chrome
 

Introduction to angular js

  • 1. INTRODUCTION TO HTML enhanced for web apps! by /Marco Vito Moscaritolo @mavimo
  • 2. <!-- DIRECTIVE: NG-MAVIMO --> { { name } }-> Marco Vito Moscaritolo { { role } }-> Developer { { company | link } }-> { { twitter | link } }-> Agavee @mavimo
  • 3. HEADS UP Starting from template... ...adding controllers... ...and filters... ...using routing... ...manage resources... ...and fun :)
  • 4. TEMPLATES (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. The directives can be placed in element names, attributes, class names, as well as comments. <span ng-repeat="exp"></span> <span class="ng-repeat: exp;"></span> <ng-repeat></ng-repeat> <!-- directive: ng-repeat exp -->
  • 5. <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angula </head> <body> <div> <input type="text" ng-model="sample" placeholder="Enter text"> <hr> <h1>!</h1> </div> </body> </html>
  • 7. <!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"> have members</li> </ul> </body> </html>
  • 8. 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
  • 10. 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>
  • 11. 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; }
  • 12. 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>
  • 13. 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; }]);
  • 14. DEPENDENCY INJECTION Dependency Injection (DI) is a software design pattern that deals with how code gets hold of its dependencies.
  • 15. SERVICES Angular services are singletons that carry out specific tasks. Eg. $http service that provides low level access to the browser's XMLHttpRequest object.
  • 16. gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) { // ... return $scope; }]); angular.module('gdgModuleAlarm', []). factory('alarm', function($window) { return { alarm: function(text) { $window.alert(text); } }; } );
  • 17. 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'];
  • 18. FILTERS Filters perform data transformation. They follow the spirit of UNIX filters and use similar syntax | (pipe).
  • 19. <!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>
  • 20. 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 Sort
  • 21. 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; }; });
  • 22. MODEL The model is the data which is merged with the template to produce the view.
  • 23. <!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angula </head> <body> <div> <input type="text" ng-model="sample" placeholder="Enter text"> <hr> <h1>!</h1> </div> </body> </html>
  • 24. 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; } ]);
  • 25. 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'}); }]);
  • 26. 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();
  • 27. 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 (r project.id = response.data.id; return project; }); }; // an instance method to pudate create a new Project Project.prototype.update = function () { var project = this; return $http.put(gdg.url + '/projects/' + project.id + '.json', project return project; }); };
  • 28. TESTING Karma - a test runner that fits all our needs. Jasmine - Jasmine is a behavior-driven development framework for testing JavaScript code.
  • 29. 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); }); it('GDG item must have members up to 0', function () { angular.forEach(scope.gdgs,function (key, gdg) { expect(gdg.members).toBeGreaterThan(0); } }); it('GDG item must have name and members', function () { angular.forEach(scope.gdgs,function (key, gdg) { expect(gdg.name).toBeDefined(); expect(gdg.members).toBeDefined();
  • 31.
  • 32. THE END MARCO VITO MOSCARITOLO / @MAVIMO