+ All Categories
Home > Technology > Introduction to AngularJS

Introduction to AngularJS

Date post: 10-May-2015
Category:
Upload: marco-moscaritolo
View: 892 times
Download: 2 times
Share this document with a friend
34
Introduction to HTML enhanced for web apps! by / Marco Vito Moscaritolo @mavimo
Transcript
Page 1: Introduction to AngularJS

Introduction to

HTML enhanced for web apps!

by / Marco Vito Moscaritolo @mavimo

Page 2: Introduction to AngularJS

<!-­-­ directive: ng-­mavimo -­-­>{ { me.name } } -> Marco Vito Moscaritolo

{ { me.role } } -> Developer{ { me.company.name | link } } -> { { me.twitter | link } } ->

Agavee@mavimo

Page 3: Introduction to AngularJS

What's AngularJSAngularJS is a toolset for building the framework most suited to

your application development.

Page 4: Introduction to AngularJS

Heads UpStarting from directives...

...adding controllers......and filters...

...using routing......manage resources...

...and fun :)

Page 5: Introduction to AngularJS

DirectivesDirectives are a way to teach HTML new tricks. During DOM

compilation directives are matched against the HTML andexecuted. 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 -->

Page 6: Introduction to AngularJS

<!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>

Page 7: Introduction to AngularJS

Write here...

Page 8: Introduction to AngularJS

<!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>

Page 9: Introduction to AngularJS

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

Page 10: Introduction to AngularJS

and alsong-showng-switchng-class...

Page 11: Introduction to AngularJS

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>

Page 12: Introduction to AngularJS

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;}

Page 13: Introduction to AngularJS

Methods in controllerfunction 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>

Page 14: Introduction to AngularJS

Controller in modulevar 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;}]);

Page 15: Introduction to AngularJS

Dependency Injection(also know as Inversion of Control)

Dependency Injection (DI) is a software design pattern thatdeals with how code gets hold of its dependencies.

Page 16: Introduction to AngularJS

ServicesAngular services are singletons that carry out specific tasks.

Eg. $http service that provides low level access to the

browser's XMLHttpRequest object.

Page 17: Introduction to AngularJS

gdgApp.controller('getTweets', ['$scope', '$http', function($scope, $http) { // ... return $scope;}]);

angular.module('gdgModuleAlarm', []). factory('alarm', function($window) { return { alarm: function(text) { $window.alert(text); } }; });

Page 18: Introduction to AngularJS

Injectors// New injector created from the previus declared module.var injector = angular.injector(['gdgModuleAlarm', 'ng']);

// Request any dependency from the injectorvar a = injector.get('alarm');

// We can also force injecting usingvar alarmFactory = function (my$window) {};alarmFactory.$inject = ['$window'];

Page 19: Introduction to AngularJS

FiltersFilters perform data transformation.

They follow the spirit of UNIX filters and use similar syntax |(pipe).

Page 20: Introduction to AngularJS

<!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>

Page 21: Introduction to AngularJS

GDG MILAN have 92 membersGDG BARCELONA have 228 membersGDG SILICON VALLEY have 171 membersGDG STOCKHOLM have 119 membersGDG HERZELIYA have 140 membersGDG AHMEDABAD have 78 membersGDG LAGOS have 115 membersGDG BANGALORE have 109 membersGDG LIMA have 175 membersGDG L-AB have 77 members

Sort

Page 22: Introduction to AngularJS

Creating custom filtersangular.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; };});

Page 23: Introduction to AngularJS

ModelThe model is the data which is merged with the template to produce the view.

Page 24: Introduction to AngularJS

<!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>

Page 25: Introduction to AngularJS

Configurationangular.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; } ]);

Page 26: Introduction to AngularJS

Routingangular.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'}); }]);

Page 27: Introduction to AngularJS

Resourceangular.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();

Page 28: Introduction to AngularJS

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;

Page 29: Introduction to AngularJS

TestingKarma - a test runner that fits all our needs.

Jasmine - Jasmine is a behavior-driven developmentframework for testing JavaScript code.

Page 30: Introduction to AngularJS

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); });

Page 31: Introduction to AngularJS

Angular 1.2...is coming!

More modularIntroduce ng-animate (and related)Support to mobile (÷/-)

Page 32: Introduction to AngularJS

Demo

Page 33: Introduction to AngularJS

?Questions time

Page 34: Introduction to AngularJS

THE ENDMarco Vito Moscaritolo / @mavimo


Recommended