47
DISCOVER ANGULARJS

Discover AngularJS

Embed Size (px)

Citation preview

Page 1: Discover AngularJS

DISCOVER ANGULARJS

Page 2: Discover AngularJS

ANGULARJS IS {{MAGIC}} !

Page 3: Discover AngularJS

INTRODUCTIONAngularJS is a framework to builda SPA (Single Page Application)

Page 4: Discover AngularJS

INTRO / ANGULARJS VS OTHERS FRAMEWORKS

Page 5: Discover AngularJS

INTRO / LEARNING CURVE

Page 6: Discover AngularJS

HEY MAN! TALK IS CHEAP.HEY MAN! TALK IS CHEAP.SHOW ME THE CODE!SHOW ME THE CODE!

Page 7: Discover AngularJS

ARCHITECTURE OF A PAGE<html ng-app="myApp">

<head> script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" /script</head>

<body ng-controller="AppController"> <h1 ng-bind="title"></h1>

script var app = angular.module('myApp', []);

app.controller('AppController', AppController);

function AppController($scope) { $scope.title = 'Hello World'; } /script</body>

</html>

Page 8: Discover AngularJS

WHAT IS DATA-BINDING ?When modifying a value in the Javascript, HTML is updated.

And vice versa.This is the digest cycle.

Page 9: Discover AngularJS

DATABINDING / DIGEST CYCLE

Page 10: Discover AngularJS

DATABINDING / DIGEST EXAMPLE<input type="text" ng-model="title" /><span ng-bind="title"></span>

1.  User add a character in the text box2.  The keydown event triggers the digest cycle3.  ng‐model modifies the $scope.title4.  $scope.title modifies ng‐bind5.  at least one value is updated: restart digest6.  nothing is updated: no more digest.

Page 11: Discover AngularJS

KNOW SOME BASIC

Page 12: Discover AngularJS

YOUR APP IS A MODULEModule = Java package

Page 13: Discover AngularJS

MODULE / DECLARATION

First, set your app as a module :angular.module( 'myApp', [ 'ui.router', '…' ]);

Second, use it to declare other parts :var app = angular.module('myApp');

Page 14: Discover AngularJS

MODULE / CONTENT

What can be declared in a module ?controllerfactorydirective

Page 15: Discover AngularJS

EMBED DATA IN A CONTROLLERThe controller binds variables between JavaScript and HTML.<div ng-controller="MyController"> <span ng-bind="myText"></span> <button ng-click="valid()">Valid Me !</button></div>

The $scope transports variables.angular.module('app').controller('MyController', MyController);

function MyController($scope) { $scope.myText = 'I am the best'; $scope.valid = function() { alert($scope.myText); };}

Page 16: Discover AngularJS

DEPENDENCY INJECTION (DI)Dependency Injection (DI) is a software design pattern thatdeals with how components get hold of their dependencies.

Page 17: Discover AngularJS

DI / COMPARE WITH JAVA

With JAVA :public class MyController { private static MyController s = null;

public static getInstance() { if ( s == null ) s = new MyController(); return s; }}

With JS + AngularJS :angular.module('app').controller('MyController', MyController);

Page 18: Discover AngularJS

MY TOOLBOX

Page 19: Discover AngularJS

STANDARD DIRECTIVESAll AngularJS directives :

can create new HTML elementcan modify the behaviour of existing elementsare prefixed with ng‐are web components (tomorrow's web)

Page 20: Discover AngularJS

STANDARD DIRECTIVES / COMMON LIST

Application ng‐app, ng‐controllerDatabinding ng‐bind, ng‐modelControl ng‐repeat, ng‐if, ng‐showEvent ng‐click, ng‐submitStyle ng‐class

Page 21: Discover AngularJS

HEY MAN! TALK IS CHEAP.HEY MAN! TALK IS CHEAP.SHOW ME SOME EXAMPLES!SHOW ME SOME EXAMPLES!

Page 22: Discover AngularJS

CUSTOM DIRECTIVESCustom directives promote your code.We can create new HTML elements !

Page 23: Discover AngularJS

OKAY! NOW, LET'S CREATEOKAY! NOW, LET'S CREATETHE <NYANCAT> DIRECTIVE!THE <NYANCAT> DIRECTIVE!

Page 24: Discover AngularJS

FILTERSFilters modify the behaviour of the databinding.

<span ng-bind="name | uppercase"></span>

Page 25: Discover AngularJS

FILTERS / EXAMPLES

AngularJS provides many filters to format :numbers (add spaces or ,.)currencies (add '$')string (lowercase, uppercase)etc.

Page 26: Discover AngularJS

FILTERS / PIPE

We can pipe multiple filters !<span ng-bind="price | currency:'euR' | uppercase"></span>

Page 27: Discover AngularJS

DO YOU WANT SOMEDO YOU WANT SOMEEXAMPLES ?EXAMPLES ?

Page 28: Discover AngularJS

SERVICES

Page 29: Discover AngularJS

SERVICES / DEFINITION

Services group business function :REST API callscommon behaviour (eg: dropdown)

Page 30: Discover AngularJS

SERVICES / EXAMPLE

Find a list of peoples :angular .module('myApp') .factory('PeopleService', PeopleService);

function PeopleService() { var factory = { findAll: findAll };

return factory;

////////////

function findAll() { var list = ['joe', 'bernard', 'sandra'];

return list; }}

Page 31: Discover AngularJS

SERVICES / CALL

Call a service from a controller :angular .module('myAll') .controller('PeopleController', PeopleController);

function PeopleController($scope, PeopleService) { $scope.people = PeopleService.findAll(); }

Page 32: Discover AngularJS

BESTPRACTICES

Page 33: Discover AngularJS

BESTPRACTICES / CONTROLLER(function() { 'use strict';

angular .module('myApp') .controller('PeopleController', PeopleController);

function PeopleController($scope) { $scope.people = ['joe', 'bernard', 'sandra']; }

})();

Page 34: Discover AngularJS

BESTPRACTICES / SERVICE(function() {'use strict';

angular .module('myApp') .factory('PeopleService', PeopleService);

function PeopleService() { var factory = { findAll: findAll };

return factory;

////////////

function findAll() { var list = ['joe', 'bernard', 'sandra'];

return list; } }

})();

Page 35: Discover AngularJS

ORGANIZE FILES

Page 36: Discover AngularJS

ORGANIZE FILES / OVERVIEWmyapp|- bower_components : external plugins|- node_modules : tools to build the project|- scss : my common styles|- src |- app : the application, organized by features |- assets : media files |- images |- common : JS helper for the app |- components : Common directives |- index.html : app entry point |- index.js : app initialisation|- bower.json : list of the external plugins|- gulpfile.js : JS makefile|- package.json : list of tools to build the project

Page 37: Discover AngularJS

ORGANIZE FILES / 'SRC/APP'myapp|- src |- app |- area |- _area.scss |- area.controller.js |- area.html |- area.route.js |- area.service.js |- home |- _home.scss |- home.controller.js |- home.html |- home.route.js

Page 38: Discover AngularJS

ORGANIZE FILES / 'SRC/COMMON'myapp|- src |- common : JS helper for the app |- map | map.helper.js

Page 39: Discover AngularJS

ORGANIZE FILES / 'SRC/COMPONENTS'myapp|- src |- components : Common directives |- chart | _chart.scss | chart.directive.js | chart.html

Page 40: Discover AngularJS

REFERENCESTutorials : angularjs.org

Styleguide John PapaStyleguide Todd MottoSnippets Sublime Text FV

Page 41: Discover AngularJS

TUTORIAL

Page 42: Discover AngularJS

TUTORIAL / GET THE PROJECT

GET THE PROJECT$ git clone https://github.com/fabienvauchelles/stweb-angularjs-tutorial.git$ cd stweb-angularjs-tutorial/frontend

Page 43: Discover AngularJS

TUTORIAL / CREATE THE MAIN APP

GET THE NEXT STEP$ git reset --hard q0

$ npm install$ bower install

$ gulp serve

WORK1.  Create the app in src/app/index.js2.  Add ng‐app to the html tag (src/app/index.html)

Page 44: Discover AngularJS

TUTORIAL / FIRST CONTROLLER

GET THE NEXT STEP$ git reset --hard q1

$ gulp serve

WORK1.  Add controller AppController in index.js2.  Reference controller on body tag (index.html)

Page 45: Discover AngularJS

TUTORIAL / NG-REPEAT

GET THE NEXT STEP$ git reset --hard q2

$ gulp serve

WORK1.  Use ng‐repeat to show many articles2.  Fill every field of an article with ng‐bind3.  Use ng‐repeat to show many tags of articles

Page 46: Discover AngularJS

TUTORIAL / FILTER

GET THE NEXT STEP$ git reset --hard q3

$ gulp serve

WORK1.  Add search field searchText with ng‐model to the input(search bar)

2.  Filter ng‐repeat with filter:searchText

WORK (BONUS)

Page 47: Discover AngularJS

© Fabien Vauchelles (2015)

TUTORIAL / SERVICE

GET THE NEXT STEP$ git reset --hard q4

$ gulp serve

WORK1.  Create an ArticlesService in common directory2.  Add the findAll function which returns all articles3.  Inject the service the AppController4.  Use service to initialize $scope.articles