angularjs 101

50
ANGULARJS 101 "Cause evrything is cool the ng-way" #GDG Blida #DEVFEST

Upload: houssem-yahiaoui

Post on 12-Jul-2015

372 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: AngularJS 101

ANGULARJS 101"Cause evrything is cool the ng-way"

#GDG Blida #DEVFEST

Page 2: AngularJS 101

#badjokes!

Page 3: AngularJS 101

HOUSSEM YAHIAOUI

AKA : EL-CODE

Student & GDG Blida member

&

MEAN stack lover & AngularJS indoor speacker

Page 4: AngularJS 101

PS: I'M NOT A PSYCHOPATH

Page 5: AngularJS 101

FRIENDLY BUT DEADLY PERSON

If you are || you know someone who like those stuff just call me at: 

+213 0 - what-ever !

Page 6: AngularJS 101

AGENDA !1. What is Angular ?2. What is a SPA3. Angularjs Anatomy :

1. App vs module

2. Services

3. Controllers

4. Factories

5. Directives

So Let's Rock in !

Page 7: AngularJS 101

First of all :

In order to make things even clearer !

Page 8: AngularJS 101

WHAT IS ANGULARJS ?what about a History lesson !

Page 9: AngularJS 101

Developed in 2009 By : Misko Hevery, publicly released in

oct 2010 version : 0.9.0 and now we're in version 1.3.6

Page 10: AngularJS 101

And his PM Said

Page 11: AngularJS 101

AngularJS is :

1. An intuitive framework that makes it easy to organizeyour code.

2. An MV* framework : MVC / MVVM / MVP ...

3. A two way data binding framework : a datasynchronization method that let the value change inmodel and view side.

Page 12: AngularJS 101

Me when i start to get along with Angularjs

Page 13: AngularJS 101

SPA !Question:

What's that ?

Page 14: AngularJS 101

Spa !"Single-Page Applications (SPAs) are Web apps that load aSingle HTML page and dynamically update that page as

the user interacts with the app."Mike Wasson

Or :

"An awesome relatively new tech for awesome cool andUI/UX capable application that makes you feel like a JS guru

and makes you looks good in public aka: "Khshine"Me

Page 15: AngularJS 101

Another Question:

Page 16: AngularJS 101

Why we don't use Jquery

Jquery capabilites are framed in the DOM manipulation sothe framework can't handle certain SPAs needed

behaviours such as routing & role decaupling aka SOC ...

Page 17: AngularJS 101

ANGULARJS ANATOMY !

Page 18: AngularJS 101

NG APP !

App or Module ?

Page 19: AngularJS 101

Simple RE : App = Module*

& remember

Page 20: AngularJS 101

The What ?

Consider the module as your container, that container canhave your application different parts : Controllers / Filters/ Services / Directives / Constants / Values / Interceptors

...

Page 21: AngularJS 101

THE HOW!Angularjs apps like many other framework have a Viewside and a Js files which attach to it and manage it so in

order to have a fully working application we need thefollowing:

Page 22: AngularJS 101

In JS Side !

// in the app.js file or what ever you want to name it

angular.module('application name', ['External modules / it's blank by default]);

//or

var app = angular.module('application name', []);  // now we use the handled variable instead  app.   *   *   *   *

Page 23: AngularJS 101

In View Side !

Template / Index / Fragment ... *<html ng‐app="Application Name">  <head>   <!‐‐ Something HERE ! ‐‐>  </head>   <body>    <!‐‐ Something HERE ! ‐‐>  </body>

Page 24: AngularJS 101

ANGULARJS SERVICES

Question in mind !

What's a service ? and why we Should use it ?

Page 25: AngularJS 101

The What ?

A substitutable objects that are wired together using DI.You can use services to organize and share code across

your appFrom the angularjs.org site

Page 26: AngularJS 101

Angular support two type of services:

1. Core services: Are the ones shiped with angular outside of the boxwhich mean that we can use them whenever we want.

- A core service is prefixed by the : "$" sign like : $http / $q / $window ...

2. Custom services: Are the ones that we write with and for angular tosuite our special needs and also we can use them whenever we want.

Page 27: AngularJS 101
Page 28: AngularJS 101

Services in Angular have two different names andmanners to code them, we have:

1. Factories

2. Services

Page 29: AngularJS 101

So far we have a anything like a SPALet's change it !

Page 30: AngularJS 101

SPAs neat functionality is Routing

Let's talk about that one

Page 31: AngularJS 101

AngularJS services are a poviders of somehow they providesomething when they called

and because of that we have the Routing servicerepresented in the officiel ngRoute module. or other the

new awesome module the ui-router module.

let's $digest that !

Page 32: AngularJS 101

From our CodeLab Code

Page 33: AngularJS 101

Let's analyse the code :

1. $stateProvider != an Angularjs Service, it's a ui-routerservice

2. the State represent a route a urlproperty in ourqueryString

3. Each route need 4 main things :

1. A Url

2. A Name

3. A Controller - we will talk about that in the next slide -

4. A template : an html page that shows our needed info

Page 34: AngularJS 101

WHATS IS A CONTROLLER!

Angularjs controller is a javascript constructor functionthat is used to augment the Angular Scope

From the angularjs.org site

Page 35: AngularJS 101

The How !

// 1/2 ways to write controllers this way let you focus on// your javascript code and push the angular configuration awayvar Classy = function($scope){   $scope.name = "houssem"; };// NG Associate the controller to the app in hand !angular.module('app').controller('Class', Classy);

Or

// 2nd way to write your angurlarjs controller using the angularjs documented wayangular.module('app').controller('Class', function($scope){   $scope.name = "houssem"; };

Page 36: AngularJS 101

But wait there is something to consider

"Optimisation"

Page 37: AngularJS 101

So after minificationWe are goins to have the folowing

var Class=function(s){s.name="houssem";};angular.module('app').controller('Class', Classy);

So one big mess of unreadable code+

Unkown ng arguments !=

Page 38: AngularJS 101
Page 39: AngularJS 101

We Need a solution !

Dont fear "DI" is here !

But How? and Who it's gonna FIX the problem!

Page 40: AngularJS 101

The How !// 1/2 ways to write controllers this way let you focus on// your javascript code and push the angular configuration away

var Classy = function($scope){   $scope.name = "houssem"; };

// 'DI' magicClassy.$inject = ['$scope'];// NG Associate the controller to the app in hand !angular.module('app').controller('Class', Classy);

so after minification we're going to have the followingvar Classy=function(s){s.name="houssem";};Classy.$inject=['$scope'];angular.module('app').controller('Class', Classy);

The result : Same mess with correct output and a happyconsole

Page 41: AngularJS 101

WHATS IS A FACTORY!

Problimatic : The Web need DATA

As a solution:

AngularJS factory can help us getting the requiried informations in order touse it in data needed places.

Page 42: AngularJS 101

The Factory syntax !

// 1/2 ways to write factories this way let you focus on// your javascript code and push the angular configuration away

var ClassyFactory = function($http){  return {   getGDGBlidaMembers : function(year){    return $http.get('/DATA ENDPOINT ROUTE/'+year);  }    };// DI ClassyFactory.$inject = ['$http'];// NG Associate the factory to the app in hand !angular.module('app').factory('ClassyFactory', ClassyFactory);

PS : we're using the revealing pattern ;-)

Page 43: AngularJS 101

But : you may say the following :

Page 44: AngularJS 101

Now we have the needed data, let's use it !

But How ?

Page 45: AngularJS 101

Simply declare it as dependency in your Controller

var Classy = function($scope, ClassyFacotry, INotify){   ClassyFactory.getGDGBlidaMembers(2014)   .success(response) {    $scope.data = response.members;    INotify.onSuccess('Data Gathring', 'Done with success !');   }.error(response){    INotify.onError('An error has occured', response.err_message + ' !');   }   });};//DIClassy.$inject = ['$scope', 'ClassyFacotry', 'INotify'];// NG Associate the controller to the app in hand !angular.module('app').controller('Class', Classy);       

Your now well covered !

Display It ^_^

Page 46: AngularJS 101

WHATS IS A DIRECTIVE !

For me a Directive is

HTML5 ++

Page 47: AngularJS 101

But What's exactly i a Directive?

A directive is a new concept introduced in Angularjs, andused to :

1. To give our Page / Template / View a new set of functionnality.

2. Let us handle our JS logic without extra work.

3. Best part is : Angularjs let and gives you the power of creatingnew directives to suit our needs.

Page 48: AngularJS 101

That's looks promising but how Exactly it works ?

1. Consider the directive as : your regular HTML5 markupAttribute.

2. Consider the directive as : your new regular HTML5markup Tag.

3. Or Both !

<input type="text" name="email" awesome‐attrib‐directive="info" required="true">         

<gdg‐awesome required="true"></gdg‐awesome>         

<gdg‐awesome grpe‐name="GDG" grpe‐loc="Blida"></gdg‐awesome>         

Page 49: AngularJS 101

Angular is already have a really cool set of core directives

And more ...

Page 50: AngularJS 101

That was it ! / Keep in touch with me !

G+ : +HoussemYahiaoui

Twittre : @free_g33k

Facebook : fcb.com/houssem.intern0t#peace