SlideShare une entreprise Scribd logo
1  sur  77
Télécharger pour lire hors ligne
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Getting started with the superheroic
JavaScript library
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Armin Rüdiger Vieweg
PHP, TYPO3, JavaScript developer
About the author
❖ 30 years old from Linz am Rhein (DE)
❖ 4.5 years experience with TYPO3
➢ published 17 extensions in TER
❖ Working with AngularJS for ~1 year
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Workshop Schedule
1. Presentation
2. Live Coding Examples
3. Practicing AngularJS
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS is made for...
❖ Single page applications (SPA, like Twitter)
❖ Weba pps (eg. with Cordova Framework)
❖ More complex magic on your web project
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Facts
❖ Also called just “Angular”
❖ Initially published in
2009
❖ Released under MIT
Licence
❖ Developed by Google
and community
❖ Website: angularjs.org
❖ Library file size (v1.2.17)
➢ 103 KiB production
➢ 749 KiB development
❖ Includes jqLite or uses
jQuery if existing
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (1/7)
❖ We need a blank HTML template
❖ And a clean folder structure:
■ app/
■ css/
■ js/
■ index.html
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (2/7)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AngularJS Workshop</title>
<link media="all" rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="js/jquery-2.1.1.min.js"></script>
<script src="js/angularjs/angular.min.js"></script>
<!-- AngularJS App -->
<script src="app/app.js"></script>
</body>
</html>
A blank HTML template
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (3/7)
❖ Container for AngularJS magic
❖ You may include other modules
➢ Don’t invent the wheel twice
➢ Just reuse other modules in your applications
❖ A module is the beginning of all AngularJS projects
A module for the application
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (4/7)
❖ In app/app.js we declare our first module:
A module for the application
// declares a module
var app = angular.module('myFirstApp', []);
app.controller(‘MyFirstController’, ...);
// also declares a module
angular.module('mySecondApp', ['myFirstApp']);
angular.module('mySecondApp').controller(...);
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (5/7)
❖ This happens with the directive ng-app:
Lets introduce the HTML to our new app
<body ng-app="myFirstApp">
<!-- Script includes ... -->
<script src="app/app.js"></script>
</body>
❖ It is possible to use several apps seperately on
one page
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (6/7)
❖ Created a blank HTML template
➢ Included jQuery, AngularJS and our first module
❖ Declared first module in app.js
❖ Paired <body> with myFirstApp
➢ by using ng-app directive
Summary
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The very first steps (7/7)
❖ An awesome blank site, ready for AngularJS magic ;-)
Result
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (1/7)
❖ Everything in your DOM may be a directive:
➢ Elements (<ng-include></ng-include)
➢ Classes (class="ng-include: data;")
➢ Attributes (<b ng-include="data">)
➢ Comments (<!-- directive: ng-include data -->)
❖ Directives attach custom behavior to those elements
or transform them
What the $%&!% are directives?!
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (2/7)
❖ AngularJS provides plenty of its own directives:
AngularJS provided directives
❖ ngApp
❖ ngBind
❖ ngBlur
❖ ngChange
❖ ngChecked
❖ ngClass
❖ ngClick
❖ ngInlcude
❖ ngModel
❖ ngPluralize
❖ ngRepeat
❖ ngShow
❖ ngSrc
❖ ...
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (3/7)
❖ Directive ngModel:
➢ <input ng-model="foo">
➢ <input data-ng:model="foo">
➢ <input data-ng-model="foo">
➢ <input ng:model="foo">
➢ <input ng_model="foo">
➢ <input x-ng-model="foo">
❖ Might be necessary for html/xhtml validation reasons
Different syntax available
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (4/7)
❖ Let’s take the HTML template we have prepared and add:
Simple example of Angular’s directives (1/2)
<input type="text" ng-model="name">
<h1 ng-show="name" ng-class="{red: name=='Armin'}">
Hello, {{name}}!
</h1>
❖ name is a new scope variable
➢ ng-model binds the value of <input> to this variable
➢ {{name}} expression outputs the variable
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (5/7)
❖ We also add a button to set name:
Simple example of Angular’s directives (2/2)
<button ng-click="name='Penelope'">Click me</button>
❖ Clicking the button will set the scope variable name to
“Penelope”. This affects:
➢ The value of <input>, because it is two-way data bound
to variable name
➢ And the {{name}} expression, which outputs the value
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (6/7)
❖ Allmost every DOM element may be a directive
❖ We have learned some of Angular’s directives:
➢ ng-model, ng-show, ng-class and ng-click
❖ We have heard about scope variables
❖ We know of double curley expressions to output
{{variables}}
Summary
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Directives (7/7)
❖ A dynamic application
without writing one line of
javascript code
Result
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives ✓
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (1/14)
1. Scopes
2. Controllers
3. Expressions
4. Two-Way Data Binding
or: Why AngularJS is superheroic!
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
$rootScope
The Big Picture (2/14)
<body ng-app="myFirstApp">
<input type="text" ng-model="name">
<button ng-click="name='Penelope'">Click me</button>
<h1 ng-show="name" ng-class="{red: name=='Armin'}">
Hello, {{name}}!
</h1>
<!-- ... -->
</body>
RootScope
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
$rootScope
The Big Picture (3/14)
scope
<body ng-app="myFirstApp">
<input type="text" ng-model="name">
<button ng-click="name='Penelope'">Click me</button>
<div ng-controller="SuperheroicController">
<h1 ng-show="name" ng-class="{red: name=='Armin'}">
Hello, {{name}}!
</h1>
</div>
<!-- ... -->
</body>
Scope inheritance
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (4/14)
❖ Controllers create new child scopes
❖ May contain:
➢ Scope variables
➢ Scope functions
❖ Should contain only business logic
➢ Set up the initial state of $scope object
➢ Add behavior to the $scope object
Controllers
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (5/14)
❖ Create file app/Controllers/Superheroic.js with
content:
Create the first controller
app.controller('SuperheroicController', ['$scope', function($scope){
$scope.name = 'Tom';
}]);
❖ Expression {{name}} inside of controller’s scope will
always return “Tom”
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (6/14)
❖ May also contain functions:
Controller’s $scope
app.controller('SuperheroicController', ['$scope', function($scope){
$scope.add = function(a, b) {
return a + b;
}
}]);
❖ Expressions may also output functions and pass
parameters:
<h1>1 plus 2 equals <em>{{add(1,2)}}</em></h1>
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (7/14)
❖ Change events for scope variables
➢ Get fired when value of defined scope variable changes
Watches
$scope.$watch('name', function(newValue, oldValue){
alert('New value: ' + newValue);
}, false);
❖ Instead of 'name' you may also use a function
❖ Can also watch deep objects (set third param to true)
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (8/14)
❖ Double curley braces syntax
➢ Contains basically javascript
➢ Accesses scope variables and scope functions
Expressions
❖ Examples:
a. {{a+b}}
b. {{alert('Does this work?')}}
c. {{'Just outputs this string'}}
d. {{a ? a : '??'}}
e. {{user.name}}
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (9/14)
Two-Way Data Binding
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (10/14)
Two-Way data binding example (with one user)
Model User
{
id: 1,
name: 'Vieweg',
firstName: 'Armin',
image: 'armin.png'
}
<div class="user">
<img ng-src="path/to/{{user.image}}">
<h2>
{{user.name}},
{{user.firstName}}
</h2>
</div>
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (11/14)
❖ Scope variables may also contain arrays of objects
❖ To work with them use the ng-repeat directive
Two-Way data binding example (with several users)
<div class="user">
<img ng-src="path/to/{{user.image}}">
<h2>{{user.name}}, {{user.firstName}}</h2>
</div>
<div class="entry" ng-repeat="user in users">
</div>
$scope.users = [
{
name: '...',
firstName: ''
},
{...}
];
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (12/14)
❖ Very helpful extension for Google Chrome (Link)
❖ Shows and highlights scopes and its variables
Google Chrome: AngularJS Batarang
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (13/14)
❖ AngularJS works with scopes
➢ Scopes inherit their variables/functions to child-scopes
➢ At the very top there exists one $rootScope
➢ $scope.$watch allows us to react on changes of variables
❖ Expressions work in scope context
➢ They check all scopes up to $rootScope for requested
variable or function
❖ Two-Way Data Binding does very much work for us
Summary
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
The Big Picture (14/14)
Result
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes
Directives ✓
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes ✓
Directives ✓
Controllers
Filters
Providers
Services Factories Validators
ExpressionsModules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes ✓
Directives ✓
Controllers
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Dependency Injection
❖ Software Design Pattern
❖ Instantiates and caches used
components
How components get ahold of their dependencies
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Dependency Injection
❖ From parameter names in functions:
Two notations to inject
app.controller('SuperheroicController', function($scope){
$scope.hello = 'world';
});
❖ Inline array notation:
app.controller('SuperheroicController', ['$scope', function(whatever)
{
whatever.hello = 'world';
}]);
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
❖ Reuseable component
❖ A service/factory in Angular is:
➢ Lazy instantiated
➢ Singleton
❖ Angular offers several useful services
➢ They are prepended with $
➢ Do not use $ in your own services
Substitutable objects that are wired together using DI
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
❖ $http - For ajax requests
❖ $interval and $timeout - Repeats and delays
❖ $rootScope - Very top scope of application
❖ $location - URL and its parts of current site
❖ $window - Wrapper of global window. Useful for tests.
Selection of useful services provided by Angular
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
Usage example (with Dependency Injection)
app.controller('SuperheroicController',
['$scope', '$http', function($scope, $http){
$scope.getTypo3Releases = function() {
$http({
method: 'GET',
url: 'http://get.typo3.org/json'
}).success(function(response){
// ...
});
};
}]);
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
Writing our first factory
app.factory('Typo3Releases', ['$http', function($http){
var getUrl = 'http://get.typo3.org/json';
var typo3ReleasesService = {};
typo3ReleasesService.get = function(callbackSuccess) {
$http({
method: 'GET',
url: getUrl
}).success(callbackSuccess);
};
return typo3ReleasesService;
}]);
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
Inject and call our first factory
app.controller('SuperheroicController',
['$scope', 'Typo3Releases', function($scope, Typo3Releases){
$scope.getTypo3Releases = function() {
Typo3Releases.get(function(response){
// ...
});
};
}]);
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Services & Factories
Service and factory syntax compared
app.service('Typo3Releases', ['$http', function($http){
this.get = function(){
// ...
}
}]);
app.factory('Typo3Releases', ['$http', function($http){
var typo3ReleasesService = {};
typo3ReleasesService.get = function() {
// ...
};
return typo3ReleasesService;
}]);
Both have the same call:
Typo3Releases.get();
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services ✓ Factories Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services ✓ Factories ✓ Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Filters
❖ Functions which modify expressions
❖ But does not touch the original data
❖ Using filters:
{{name | filter1 | filter2:option}}
Modify expressions
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Filters
❖ AngularJS provides few of its own filters:
AngularJS provided filters
❖ currency
❖ date
❖ filter
❖ json
❖ limitTo
❖ lowercase
❖ number
❖ orderBy
❖ uppercase
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Filters
❖ {{price | currency:'€'}} // €1,234.56
❖ {{name | uppercase}} // ARMIN
❖ {{created | date:'dd.MM.yyyy'}} // 20.06.2014
❖ Options of filters may be filled by scope variable:
{{created | date:format}}
Usage examples
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Filters
Writing your own filters
app.filter('replaceVowelsWith', function(){
return function(input, option){
if (option === undefined || input === undefined) return input;
return input.replace(/[aeiou]/gi, option);
}
});
{{'Drei Chinesen mit dem Kontrabass...' | 'e'}}
Dree Chenesen met dem Kentrebess...
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters
Providers
Services ✓ Factories ✓ Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters ✓
Providers
Services ✓ Factories ✓ Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Validators
❖ Forms and/or fields get validated
❖ By HTML5 validation notation (eg. type="email")
❖ Independent from browser validation, Angular:
➢ Checks values on its own
➢ Adds indicating classes to fields and forms (eg. ng-invalid)
➢ Adds $invalid property to scope of form
❖ You may write your own validators using directives
You're not coming in
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Validators
<form name="form" novalidate>
<input type="email" ng-model="mail" name="mail" required>
<button type="submit" ng-disabled="form.$invalid">Submit</button>
</form>
Example
Show error messages in case validators fail:
<span ng-if="form.mail.$error.required">Mail is required!</span>
<span ng-if="form.mail.$error.email">No valid mail!</span>
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Validators
❖ Writing a validator means writing a directive
❖ Usage example in template:
➢ <input name="number" type="number" ng-model="number"
required even-number>
➢ Input must be
✓ any input (required)
✓ a number (type="number")
✓ an even number (directive even-number)
Your own validators/directives
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters ✓
Providers
Services ✓ Factories ✓ Validators
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters ✓
Providers
Services ✓ Factories ✓ Validators ✓
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Providers
❖ Five recipes for providers:
1. Value
2. Constant
3. Factory
4. Service
5. Provider
❖ Providers are bound to modules/applications
Almost every AngularJS buzzword is made by providers
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Providers
var app = angular.module('myFirstApp', []);
app.value('bestCmsEver', 'TYPO3');
app.controller('SuperheroicController', ['$scope', 'bestCmsEver',
function($scope, bestCmsEver){
this.bestCmsEver = bestCmsEver;
}]);
Small example with value and constant provider
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters ✓
Providers
Services ✓ Factories ✓ Validators ✓
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS Buzzwords
Dependency Injection ✓
Two-way Data Binding ✓
Scopes ✓
Directives ✓
Controllers ✓
Filters ✓
Providers ✓
Services ✓ Factories ✓ Validators ✓
Expressions ✓Modules ✓
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Advantages of AngularJS
❖ Allows you to work clean using reuseable modules
❖ Features of Angular
➢ enables completely new possibilites (2-way data binding)
➢ saves a lot of time for common tasks (like validation)
❖ Components are unittestable
❖ Further development of Angular, thanks to Google
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
AngularJS help
❖ Guide: https://docs.angularjs.org/guide
❖ API: https://docs.angularjs.org/api
❖ Many many articles, videos and examples on
➢ YouTube
➢ StackOverflow
➢ all over the web
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Workshop Schedule
1. Presentation
2. Live Coding Examples
3. Practicing AngularJS
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Workshop Schedule
1. Presentation ✓
2. Live Coding Examples
3. Practicing AngularJS
15 minute break
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS
Thanks for your ttention!
20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Contenu connexe

Tendances

Overview about AngularJS Framework
Overview about AngularJS Framework Overview about AngularJS Framework
Overview about AngularJS Framework Camilo Lopes
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSDavid Parsons
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginnersMunir Hoque
 
Introduction to AngularJS Framework
Introduction to AngularJS FrameworkIntroduction to AngularJS Framework
Introduction to AngularJS FrameworkRaveendra R
 
Introduction to Angular js 2.0
Introduction to Angular js 2.0Introduction to Angular js 2.0
Introduction to Angular js 2.0Nagaraju Sangam
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS introdizabl
 
Single Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSingle Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSparkhound Inc.
 
The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014Matt Raible
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - IntroductionSagar Acharya
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJSInfragistics
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introductionLuigi De Russis
 
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
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular jsTamer Solieman
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 

Tendances (20)

AngularJS is awesome
AngularJS is awesomeAngularJS is awesome
AngularJS is awesome
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
Overview about AngularJS Framework
Overview about AngularJS Framework Overview about AngularJS Framework
Overview about AngularJS Framework
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Angular js for beginners
Angular js for beginnersAngular js for beginners
Angular js for beginners
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Introduction to AngularJS Framework
Introduction to AngularJS FrameworkIntroduction to AngularJS Framework
Introduction to AngularJS Framework
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 
Introduction to Angular js 2.0
Introduction to Angular js 2.0Introduction to Angular js 2.0
Introduction to Angular js 2.0
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS intro
 
Single Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with AngularSingle Page Applications in SharePoint with Angular
Single Page Applications in SharePoint with Angular
 
The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014The Art of AngularJS - DeRailed 2014
The Art of AngularJS - DeRailed 2014
 
Angular JS - Introduction
Angular JS - IntroductionAngular JS - Introduction
Angular JS - Introduction
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
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
 
Introduction of angular js
Introduction of angular jsIntroduction of angular js
Introduction of angular js
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 

En vedette

The Art of AngularJS in 2015
The Art of AngularJS in 2015The Art of AngularJS in 2015
The Art of AngularJS in 2015Matt Raible
 
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST API
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST APISPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST API
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST APIEric Shupps
 
SharePoint is Talking - Are you Listening
SharePoint is Talking - Are you ListeningSharePoint is Talking - Are you Listening
SharePoint is Talking - Are you ListeningEric Shupps
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionDzmitry Ivashutsin
 
AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injectionAlexe Bogdan
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basicsRavindra K
 
A different thought AngularJS
A different thought AngularJSA different thought AngularJS
A different thought AngularJSAmit Thakkar
 
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...IstvanKoren
 
Modern Front-End Development
Modern Front-End DevelopmentModern Front-End Development
Modern Front-End Developmentmwrather
 
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft Graph
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft GraphSharePoint Saturday Paris 2016 - AngularJS with the Microsoft Graph
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft GraphSébastien Levert
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBApaichon Punopas
 
Single page application
Single page applicationSingle page application
Single page applicationArthur Fung
 
Let your website a ride of AngularJS
Let your website a ride of AngularJSLet your website a ride of AngularJS
Let your website a ride of AngularJSMike Taylor
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScriptTodd Anglin
 

En vedette (20)

AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
The Art of AngularJS in 2015
The Art of AngularJS in 2015The Art of AngularJS in 2015
The Art of AngularJS in 2015
 
Marrying together the worlds of ADF and HTML5 & AngularJS - Oracle OpenWorld ...
Marrying together the worlds of ADF and HTML5 & AngularJS - Oracle OpenWorld ...Marrying together the worlds of ADF and HTML5 & AngularJS - Oracle OpenWorld ...
Marrying together the worlds of ADF and HTML5 & AngularJS - Oracle OpenWorld ...
 
Angularjs
AngularjsAngularjs
Angularjs
 
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST API
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST APISPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST API
SPTECHCON - Get Some REST - Taking Advantage of the SharePoint 2013 REST API
 
SharePoint is Talking - Are you Listening
SharePoint is Talking - Are you ListeningSharePoint is Talking - Are you Listening
SharePoint is Talking - Are you Listening
 
ngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency InjectionngMess: AngularJS Dependency Injection
ngMess: AngularJS Dependency Injection
 
AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injection
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
A different thought AngularJS
A different thought AngularJSA different thought AngularJS
A different thought AngularJS
 
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...
Requirements Bazaar powered by AngularJS and Polymer - Talk at Google Develop...
 
AngularJS
AngularJSAngularJS
AngularJS
 
Modern Front-End Development
Modern Front-End DevelopmentModern Front-End Development
Modern Front-End Development
 
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft Graph
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft GraphSharePoint Saturday Paris 2016 - AngularJS with the Microsoft Graph
SharePoint Saturday Paris 2016 - AngularJS with the Microsoft Graph
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
 
Single page application
Single page applicationSingle page application
Single page application
 
Let your website a ride of AngularJS
Let your website a ride of AngularJSLet your website a ride of AngularJS
Let your website a ride of AngularJS
 
The AngularJS way
The AngularJS wayThe AngularJS way
The AngularJS way
 
Angular JS
Angular JSAngular JS
Angular JS
 
5 Tips for Better JavaScript
5 Tips for Better JavaScript5 Tips for Better JavaScript
5 Tips for Better JavaScript
 

Similaire à Gettings started with the superheroic JavaScript library AngularJS

Front-end for Java developers Devoxx France 2018
Front-end for Java developers Devoxx France 2018Front-end for Java developers Devoxx France 2018
Front-end for Java developers Devoxx France 2018Deepu K Sasidharan
 
Getting Started With AngularJS
Getting Started With AngularJSGetting Started With AngularJS
Getting Started With AngularJSOmnia Helmi
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Naveen Pete
 
Fast prototyping apps using AngularJS, RequireJS and Twitter Bootstrap
Fast prototyping apps using AngularJS, RequireJS and Twitter BootstrapFast prototyping apps using AngularJS, RequireJS and Twitter Bootstrap
Fast prototyping apps using AngularJS, RequireJS and Twitter BootstrapYuriy Silvestrov
 
Create responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJSCreate responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJSHannes Hapke
 
Top 5 AngularJS Tool for Application Development
Top 5 AngularJS Tool for Application DevelopmentTop 5 AngularJS Tool for Application Development
Top 5 AngularJS Tool for Application DevelopmentPaul Cook
 
JS FAST Prototyping with AngularJS & RequireJS
JS FAST Prototyping with AngularJS & RequireJSJS FAST Prototyping with AngularJS & RequireJS
JS FAST Prototyping with AngularJS & RequireJSYuriy Silvestrov
 
Swagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestSwagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestLaunchAny
 
AngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesAngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesTechtic Solutions
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsYakov Fain
 
AngularJS Project Setup step-by- step guide - RapidValue Solutions
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
 
AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?Tom Hombergs
 
Angular, the New Angular JS
Angular, the New Angular JSAngular, the New Angular JS
Angular, the New Angular JSKenzan
 
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014Endava
 
One Weekend With AngularJS
One Weekend With AngularJSOne Weekend With AngularJS
One Weekend With AngularJSYashobanta Bai
 

Similaire à Gettings started with the superheroic JavaScript library AngularJS (20)

Front-end for Java developers Devoxx France 2018
Front-end for Java developers Devoxx France 2018Front-end for Java developers Devoxx France 2018
Front-end for Java developers Devoxx France 2018
 
Getting Started With AngularJS
Getting Started With AngularJSGetting Started With AngularJS
Getting Started With AngularJS
 
AngularJS 101
AngularJS 101AngularJS 101
AngularJS 101
 
Angular JS
Angular JSAngular JS
Angular JS
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Fast prototyping apps using AngularJS, RequireJS and Twitter Bootstrap
Fast prototyping apps using AngularJS, RequireJS and Twitter BootstrapFast prototyping apps using AngularJS, RequireJS and Twitter Bootstrap
Fast prototyping apps using AngularJS, RequireJS and Twitter Bootstrap
 
Intro to AngularJS
Intro to AngularJSIntro to AngularJS
Intro to AngularJS
 
Angular js
Angular jsAngular js
Angular js
 
Create responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJSCreate responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJS
 
Top 5 AngularJS Tool for Application Development
Top 5 AngularJS Tool for Application DevelopmentTop 5 AngularJS Tool for Application Development
Top 5 AngularJS Tool for Application Development
 
JS FAST Prototyping with AngularJS & RequireJS
JS FAST Prototyping with AngularJS & RequireJSJS FAST Prototyping with AngularJS & RequireJS
JS FAST Prototyping with AngularJS & RequireJS
 
Swagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestSwagger 2.0: Latest and Greatest
Swagger 2.0: Latest and Greatest
 
AngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesAngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the Differences
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
AngularJS Project Setup step-by- step guide - RapidValue Solutions
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 Solutions
 
AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?AngularJS - The Next Big Thing?
AngularJS - The Next Big Thing?
 
Angular, the New Angular JS
Angular, the New Angular JSAngular, the New Angular JS
Angular, the New Angular JS
 
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014
Angularjs vs Dojo toolkit | SuperSpeaker@CodeCamp Iasi 2014
 
Angular js workshop
Angular js workshopAngular js workshop
Angular js workshop
 
One Weekend With AngularJS
One Weekend With AngularJSOne Weekend With AngularJS
One Weekend With AngularJS
 

Gettings started with the superheroic JavaScript library AngularJS

  • 1. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Getting started with the superheroic JavaScript library
  • 2. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Armin Rüdiger Vieweg PHP, TYPO3, JavaScript developer About the author ❖ 30 years old from Linz am Rhein (DE) ❖ 4.5 years experience with TYPO3 ➢ published 17 extensions in TER ❖ Working with AngularJS for ~1 year
  • 3. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Workshop Schedule 1. Presentation 2. Live Coding Examples 3. Practicing AngularJS
  • 4. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS is made for... ❖ Single page applications (SPA, like Twitter) ❖ Weba pps (eg. with Cordova Framework) ❖ More complex magic on your web project
  • 5. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Facts ❖ Also called just “Angular” ❖ Initially published in 2009 ❖ Released under MIT Licence ❖ Developed by Google and community ❖ Website: angularjs.org ❖ Library file size (v1.2.17) ➢ 103 KiB production ➢ 749 KiB development ❖ Includes jqLite or uses jQuery if existing
  • 6. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives Controllers Filters Providers Services Factories Validators ExpressionsModules
  • 7. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (1/7) ❖ We need a blank HTML template ❖ And a clean folder structure: ■ app/ ■ css/ ■ js/ ■ index.html
  • 8. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (2/7) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>AngularJS Workshop</title> <link media="all" rel="stylesheet" href="css/style.css"> </head> <body> <script src="js/jquery-2.1.1.min.js"></script> <script src="js/angularjs/angular.min.js"></script> <!-- AngularJS App --> <script src="app/app.js"></script> </body> </html> A blank HTML template
  • 9. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (3/7) ❖ Container for AngularJS magic ❖ You may include other modules ➢ Don’t invent the wheel twice ➢ Just reuse other modules in your applications ❖ A module is the beginning of all AngularJS projects A module for the application
  • 10. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (4/7) ❖ In app/app.js we declare our first module: A module for the application // declares a module var app = angular.module('myFirstApp', []); app.controller(‘MyFirstController’, ...); // also declares a module angular.module('mySecondApp', ['myFirstApp']); angular.module('mySecondApp').controller(...);
  • 11. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (5/7) ❖ This happens with the directive ng-app: Lets introduce the HTML to our new app <body ng-app="myFirstApp"> <!-- Script includes ... --> <script src="app/app.js"></script> </body> ❖ It is possible to use several apps seperately on one page
  • 12. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (6/7) ❖ Created a blank HTML template ➢ Included jQuery, AngularJS and our first module ❖ Declared first module in app.js ❖ Paired <body> with myFirstApp ➢ by using ng-app directive Summary
  • 13. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The very first steps (7/7) ❖ An awesome blank site, ready for AngularJS magic ;-) Result
  • 14. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives Controllers Filters Providers Services Factories Validators ExpressionsModules
  • 15. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives Controllers Filters Providers Services Factories Validators ExpressionsModules ✓
  • 16. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (1/7) ❖ Everything in your DOM may be a directive: ➢ Elements (<ng-include></ng-include) ➢ Classes (class="ng-include: data;") ➢ Attributes (<b ng-include="data">) ➢ Comments (<!-- directive: ng-include data -->) ❖ Directives attach custom behavior to those elements or transform them What the $%&!% are directives?!
  • 17. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (2/7) ❖ AngularJS provides plenty of its own directives: AngularJS provided directives ❖ ngApp ❖ ngBind ❖ ngBlur ❖ ngChange ❖ ngChecked ❖ ngClass ❖ ngClick ❖ ngInlcude ❖ ngModel ❖ ngPluralize ❖ ngRepeat ❖ ngShow ❖ ngSrc ❖ ...
  • 18. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (3/7) ❖ Directive ngModel: ➢ <input ng-model="foo"> ➢ <input data-ng:model="foo"> ➢ <input data-ng-model="foo"> ➢ <input ng:model="foo"> ➢ <input ng_model="foo"> ➢ <input x-ng-model="foo"> ❖ Might be necessary for html/xhtml validation reasons Different syntax available
  • 19. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (4/7) ❖ Let’s take the HTML template we have prepared and add: Simple example of Angular’s directives (1/2) <input type="text" ng-model="name"> <h1 ng-show="name" ng-class="{red: name=='Armin'}"> Hello, {{name}}! </h1> ❖ name is a new scope variable ➢ ng-model binds the value of <input> to this variable ➢ {{name}} expression outputs the variable
  • 20. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (5/7) ❖ We also add a button to set name: Simple example of Angular’s directives (2/2) <button ng-click="name='Penelope'">Click me</button> ❖ Clicking the button will set the scope variable name to “Penelope”. This affects: ➢ The value of <input>, because it is two-way data bound to variable name ➢ And the {{name}} expression, which outputs the value
  • 21. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (6/7) ❖ Allmost every DOM element may be a directive ❖ We have learned some of Angular’s directives: ➢ ng-model, ng-show, ng-class and ng-click ❖ We have heard about scope variables ❖ We know of double curley expressions to output {{variables}} Summary
  • 22. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Directives (7/7) ❖ A dynamic application without writing one line of javascript code Result
  • 23. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives Controllers Filters Providers Services Factories Validators ExpressionsModules ✓
  • 24. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives ✓ Controllers Filters Providers Services Factories Validators ExpressionsModules ✓
  • 25. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (1/14) 1. Scopes 2. Controllers 3. Expressions 4. Two-Way Data Binding or: Why AngularJS is superheroic!
  • 26. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS $rootScope The Big Picture (2/14) <body ng-app="myFirstApp"> <input type="text" ng-model="name"> <button ng-click="name='Penelope'">Click me</button> <h1 ng-show="name" ng-class="{red: name=='Armin'}"> Hello, {{name}}! </h1> <!-- ... --> </body> RootScope
  • 27. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS $rootScope The Big Picture (3/14) scope <body ng-app="myFirstApp"> <input type="text" ng-model="name"> <button ng-click="name='Penelope'">Click me</button> <div ng-controller="SuperheroicController"> <h1 ng-show="name" ng-class="{red: name=='Armin'}"> Hello, {{name}}! </h1> </div> <!-- ... --> </body> Scope inheritance
  • 28. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (4/14) ❖ Controllers create new child scopes ❖ May contain: ➢ Scope variables ➢ Scope functions ❖ Should contain only business logic ➢ Set up the initial state of $scope object ➢ Add behavior to the $scope object Controllers
  • 29. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (5/14) ❖ Create file app/Controllers/Superheroic.js with content: Create the first controller app.controller('SuperheroicController', ['$scope', function($scope){ $scope.name = 'Tom'; }]); ❖ Expression {{name}} inside of controller’s scope will always return “Tom”
  • 30. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (6/14) ❖ May also contain functions: Controller’s $scope app.controller('SuperheroicController', ['$scope', function($scope){ $scope.add = function(a, b) { return a + b; } }]); ❖ Expressions may also output functions and pass parameters: <h1>1 plus 2 equals <em>{{add(1,2)}}</em></h1>
  • 31. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (7/14) ❖ Change events for scope variables ➢ Get fired when value of defined scope variable changes Watches $scope.$watch('name', function(newValue, oldValue){ alert('New value: ' + newValue); }, false); ❖ Instead of 'name' you may also use a function ❖ Can also watch deep objects (set third param to true)
  • 32. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (8/14) ❖ Double curley braces syntax ➢ Contains basically javascript ➢ Accesses scope variables and scope functions Expressions ❖ Examples: a. {{a+b}} b. {{alert('Does this work?')}} c. {{'Just outputs this string'}} d. {{a ? a : '??'}} e. {{user.name}}
  • 33. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (9/14) Two-Way Data Binding
  • 34. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (10/14) Two-Way data binding example (with one user) Model User { id: 1, name: 'Vieweg', firstName: 'Armin', image: 'armin.png' } <div class="user"> <img ng-src="path/to/{{user.image}}"> <h2> {{user.name}}, {{user.firstName}} </h2> </div>
  • 35. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (11/14) ❖ Scope variables may also contain arrays of objects ❖ To work with them use the ng-repeat directive Two-Way data binding example (with several users) <div class="user"> <img ng-src="path/to/{{user.image}}"> <h2>{{user.name}}, {{user.firstName}}</h2> </div> <div class="entry" ng-repeat="user in users"> </div> $scope.users = [ { name: '...', firstName: '' }, {...} ];
  • 36. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (12/14) ❖ Very helpful extension for Google Chrome (Link) ❖ Shows and highlights scopes and its variables Google Chrome: AngularJS Batarang
  • 37. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (13/14) ❖ AngularJS works with scopes ➢ Scopes inherit their variables/functions to child-scopes ➢ At the very top there exists one $rootScope ➢ $scope.$watch allows us to react on changes of variables ❖ Expressions work in scope context ➢ They check all scopes up to $rootScope for requested variable or function ❖ Two-Way Data Binding does very much work for us Summary
  • 38. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS The Big Picture (14/14) Result
  • 39. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes Directives ✓ Controllers Filters Providers Services Factories Validators ExpressionsModules ✓
  • 40. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes ✓ Directives ✓ Controllers Filters Providers Services Factories Validators ExpressionsModules ✓
  • 41. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes ✓ Directives ✓ Controllers Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 42. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 43. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 44. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Dependency Injection ❖ Software Design Pattern ❖ Instantiates and caches used components How components get ahold of their dependencies
  • 45. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Dependency Injection ❖ From parameter names in functions: Two notations to inject app.controller('SuperheroicController', function($scope){ $scope.hello = 'world'; }); ❖ Inline array notation: app.controller('SuperheroicController', ['$scope', function(whatever) { whatever.hello = 'world'; }]);
  • 46. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 47. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 48. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories ❖ Reuseable component ❖ A service/factory in Angular is: ➢ Lazy instantiated ➢ Singleton ❖ Angular offers several useful services ➢ They are prepended with $ ➢ Do not use $ in your own services Substitutable objects that are wired together using DI
  • 49. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories ❖ $http - For ajax requests ❖ $interval and $timeout - Repeats and delays ❖ $rootScope - Very top scope of application ❖ $location - URL and its parts of current site ❖ $window - Wrapper of global window. Useful for tests. Selection of useful services provided by Angular
  • 50. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories Usage example (with Dependency Injection) app.controller('SuperheroicController', ['$scope', '$http', function($scope, $http){ $scope.getTypo3Releases = function() { $http({ method: 'GET', url: 'http://get.typo3.org/json' }).success(function(response){ // ... }); }; }]);
  • 51. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories Writing our first factory app.factory('Typo3Releases', ['$http', function($http){ var getUrl = 'http://get.typo3.org/json'; var typo3ReleasesService = {}; typo3ReleasesService.get = function(callbackSuccess) { $http({ method: 'GET', url: getUrl }).success(callbackSuccess); }; return typo3ReleasesService; }]);
  • 52. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories Inject and call our first factory app.controller('SuperheroicController', ['$scope', 'Typo3Releases', function($scope, Typo3Releases){ $scope.getTypo3Releases = function() { Typo3Releases.get(function(response){ // ... }); }; }]);
  • 53. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Services & Factories Service and factory syntax compared app.service('Typo3Releases', ['$http', function($http){ this.get = function(){ // ... } }]); app.factory('Typo3Releases', ['$http', function($http){ var typo3ReleasesService = {}; typo3ReleasesService.get = function() { // ... }; return typo3ReleasesService; }]); Both have the same call: Typo3Releases.get();
  • 54. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services Factories Validators Expressions ✓Modules ✓
  • 55. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services ✓ Factories Validators Expressions ✓Modules ✓
  • 56. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services ✓ Factories ✓ Validators Expressions ✓Modules ✓
  • 57. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Filters ❖ Functions which modify expressions ❖ But does not touch the original data ❖ Using filters: {{name | filter1 | filter2:option}} Modify expressions
  • 58. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Filters ❖ AngularJS provides few of its own filters: AngularJS provided filters ❖ currency ❖ date ❖ filter ❖ json ❖ limitTo ❖ lowercase ❖ number ❖ orderBy ❖ uppercase
  • 59. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Filters ❖ {{price | currency:'€'}} // €1,234.56 ❖ {{name | uppercase}} // ARMIN ❖ {{created | date:'dd.MM.yyyy'}} // 20.06.2014 ❖ Options of filters may be filled by scope variable: {{created | date:format}} Usage examples
  • 60. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Filters Writing your own filters app.filter('replaceVowelsWith', function(){ return function(input, option){ if (option === undefined || input === undefined) return input; return input.replace(/[aeiou]/gi, option); } }); {{'Drei Chinesen mit dem Kontrabass...' | 'e'}} Dree Chenesen met dem Kentrebess...
  • 61. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters Providers Services ✓ Factories ✓ Validators Expressions ✓Modules ✓
  • 62. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters ✓ Providers Services ✓ Factories ✓ Validators Expressions ✓Modules ✓
  • 63. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Validators ❖ Forms and/or fields get validated ❖ By HTML5 validation notation (eg. type="email") ❖ Independent from browser validation, Angular: ➢ Checks values on its own ➢ Adds indicating classes to fields and forms (eg. ng-invalid) ➢ Adds $invalid property to scope of form ❖ You may write your own validators using directives You're not coming in
  • 64. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Validators <form name="form" novalidate> <input type="email" ng-model="mail" name="mail" required> <button type="submit" ng-disabled="form.$invalid">Submit</button> </form> Example Show error messages in case validators fail: <span ng-if="form.mail.$error.required">Mail is required!</span> <span ng-if="form.mail.$error.email">No valid mail!</span>
  • 65. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Validators ❖ Writing a validator means writing a directive ❖ Usage example in template: ➢ <input name="number" type="number" ng-model="number" required even-number> ➢ Input must be ✓ any input (required) ✓ a number (type="number") ✓ an even number (directive even-number) Your own validators/directives
  • 66. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters ✓ Providers Services ✓ Factories ✓ Validators Expressions ✓Modules ✓
  • 67. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters ✓ Providers Services ✓ Factories ✓ Validators ✓ Expressions ✓Modules ✓
  • 68. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Providers ❖ Five recipes for providers: 1. Value 2. Constant 3. Factory 4. Service 5. Provider ❖ Providers are bound to modules/applications Almost every AngularJS buzzword is made by providers
  • 69. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Providers var app = angular.module('myFirstApp', []); app.value('bestCmsEver', 'TYPO3'); app.controller('SuperheroicController', ['$scope', 'bestCmsEver', function($scope, bestCmsEver){ this.bestCmsEver = bestCmsEver; }]); Small example with value and constant provider
  • 70. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters ✓ Providers Services ✓ Factories ✓ Validators ✓ Expressions ✓Modules ✓
  • 71. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS Buzzwords Dependency Injection ✓ Two-way Data Binding ✓ Scopes ✓ Directives ✓ Controllers ✓ Filters ✓ Providers ✓ Services ✓ Factories ✓ Validators ✓ Expressions ✓Modules ✓
  • 72. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Advantages of AngularJS ❖ Allows you to work clean using reuseable modules ❖ Features of Angular ➢ enables completely new possibilites (2-way data binding) ➢ saves a lot of time for common tasks (like validation) ❖ Components are unittestable ❖ Further development of Angular, thanks to Google
  • 73. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS AngularJS help ❖ Guide: https://docs.angularjs.org/guide ❖ API: https://docs.angularjs.org/api ❖ Many many articles, videos and examples on ➢ YouTube ➢ StackOverflow ➢ all over the web
  • 74. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Workshop Schedule 1. Presentation 2. Live Coding Examples 3. Practicing AngularJS
  • 75. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Workshop Schedule 1. Presentation ✓ 2. Live Coding Examples 3. Practicing AngularJS 15 minute break
  • 76. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS Thanks for your ttention!
  • 77. 20th June 2014, Armin Rüdiger Vieweg (@ArminVieweg) Getting started with superheroic JavaScript library AngularJS