Вы находитесь на странице: 1из 77

Getting started with the superheroic

JavaScript library

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

About the author


Armin Rdiger Vieweg
PHP, TYPO3, JavaScript developer

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 Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Workshop Schedule
1. Presentation
2. Live Coding Examples
3. Practicing AngularJS

20th June 2014, Armin Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Facts
Also called just Angular Website: angularjs.org
Initially published in
2009
Released under MIT
Licence
Developed by Google
and community

Library file size (v1.2.17)

103 KiB production


749 KiB development

Includes jqLite or uses


jQuery if existing

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords

Two-

Modules

way D

s
e
v
i
ect

Dir

ection

nj
I
y
c
n
de
n
e
p
e
D

Providers

Expressions

Scopes

Services

ata B

indin

llers
o
r
t
Con

Filters

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The very first steps (2/7)


A blank HTML template
<!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>
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The very first steps (3/7)


A module for the application

Container for AngularJS magic


You may include other modules
Dont invent the wheel twice
Just reuse other modules in your applications

A module is the beginning of all AngularJS projects

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The very first steps (4/7)


A module for the application

In app/app.js we declare our first module:


// 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The very first steps (5/7)


Lets introduce the HTML to our new app

This happens with the directive ng-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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The very first steps (6/7)


Summary

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

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The very first steps (7/7)


Result

An awesome blank site, ready for AngularJS magic ;-)

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords

Two-

Modules

way D

s
e
v
i
ect

Dir

ection

nj
I
y
c
n
de
n
e
p
e
D

Providers

Expressions

Scopes

Services

ata B

indin

llers
o
r
t
Con

Filters

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding
s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes
Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Directives (1/7)
What the $%&!% are directives?!

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
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (2/7)
AngularJS provided directives

AngularJS provides plenty of its own directives:


ngApp

ngClass

ngRepeat

ngBind

ngClick

ngShow

ngBlur

ngInlcude

ngSrc

ngChange

ngModel

...

ngChecked

ngPluralize
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (3/7)
Different syntax available

Directive ngModel:

<input
<input
<input
<input
<input
<input

ng-model="foo">
data-ng:model="foo">
data-ng-model="foo">
ng:model="foo">
ng_model="foo">
x-ng-model="foo">

Might be necessary for html/xhtml validation reasons


20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (4/7)
Simple example of Angulars directives (1/2)

Lets take the HTML template we have prepared and add:


<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 Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (5/7)
Simple example of Angulars directives (2/2)

We also add a button to set name:


<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 Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (6/7)
Summary

Allmost every DOM element may be a directive


We have learned some of Angulars 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}}
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Directives (7/7)
Result

A dynamic application
without writing one line of
javascript code

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding
s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes
Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes
Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (1/14)


or: Why AngularJS is superheroic!

1. Scopes
2. Controllers
3. Expressions
4. Two-Way Data Binding

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (2/14)


RootScope
<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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (3/14)


Scope inheritance
$rootScope

<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

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (4/14)


Controllers

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
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (5/14)


Create the first controller

Create file app/Controllers/Superheroic.js with


content:
app.controller('SuperheroicController', ['$scope', function($scope){
$scope.name = 'Tom';
}]);

Expression {{name}} inside of controllers scope will


always return Tom
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (6/14)


Controllers $scope

May also contain functions:


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 Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (7/14)


Watches

Change events for scope variables


Get fired when value of defined scope variable changes
$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 Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (8/14)


Expressions

Double curley braces syntax


Contains basically javascript
Accesses scope variables and scope functions

Examples:
a.
b.
c.
d.
e.

{{a+b}}
{{alert('Does this work?')}}
{{'Just outputs this string'}}
{{a ? a : '??'}}
{{user.name}}
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (9/14)


Two-Way Data Binding

20th June 2014, Armin Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (11/14)


Two-Way data binding example (with several users)

Scope variables may also contain arrays of objects


To work with them use the ng-repeat directive
$scope.users = [
{
name: '...',
firstName: ''
},
{...}
];

<div class="entry" ng-repeat="user in users">


<div class="user">
<img ng-src="path/to/{{user.image}}">
<h2>{{user.name}}, {{user.firstName}}</h2>
</div>
</div>

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (12/14)


Google Chrome: AngularJS Batarang

Very helpful extension for Google Chrome (Link)


Shows and highlights scopes and its variables

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

The Big Picture (13/14)


Summary

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


20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

The Big Picture (14/14)


Result

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes
Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e
v
i
t
on
i
c
t
c
e
e
ers
j
l
r
l
n
i
I
o
r
y
t
D
nc
e
Con
d
n
e
Dep
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e
v
i
t
on
i
c
t
c
rs
e
e
e
j
l
r
l
n
i
I
o
cy
D
n
ontr
e
C
d
n
Depe
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

v
i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Dependency Injection
How components get ahold of their dependencies

Software Design Pattern


Instantiates and caches used
components

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Dependency Injection
Two notations to inject

From parameter names in functions:


app.controller('SuperheroicController', function($scope){
$scope.hello = 'world';
});

Inline array notation:


app.controller('SuperheroicController', ['$scope', function(whatever)
{
whatever.hello = 'world';
}]);
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

v
i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Services & Factories


Substitutable objects that are wired together using DI

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
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Services & Factories


Selection of useful services provided by Angular

$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.
20th June 2014, Armin Rdiger 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 Rdiger 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 Rdiger 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 Rdiger 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() {
// ...
Both have the same call:
};
return typo3ReleasesService;
Typo3Releases.get();
}]);
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

s
e
i
r
o
t
Fac

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
o
t
c

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Filters
Modify expressions

Functions which modify expressions


But does not touch the original data
Using filters:
{{name | filter1 | filter2:option}}

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Filters
AngularJS provided filters

AngularJS provides few of its own filters:


currency

lowercase

date

number

filter

orderBy

json

uppercase

limitTo
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Filters
Usage examples

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

20th June 2014, Armin Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Validators
You're not coming in

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


20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Validators
Example
<form name="form" novalidate>
<input type="email" ng-model="mail" name="mail" required>
<button type="submit" ng-disabled="form.$invalid">Submit</button>
</form>

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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Validators
Your own validators/directives

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)
20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid

ator

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid
a

tors

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Providers
Almost every AngularJS buzzword is made by providers

Five recipes for providers:


1.
2.
3.
4.
5.

Value
Constant
Factory
Service
Provider

Providers are bound to modules/applications


20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)
Getting started with superheroic JavaScript library AngularJS

Providers
Small example with value and constant provider
var app = angular.module('myFirstApp', []);
app.value('bestCmsEver', 'TYPO3');

app.controller('SuperheroicController', ['$scope', 'bestCmsEver',


function($scope, bestCmsEver){
this.bestCmsEver = bestCmsEver;
}]);

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters
Providers

Services

Fa

s
e
i
r
cto

Valid
a

tors

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

AngularJS Buzzwords
Expressions
Modules Two-wa
y Dat
a Bin
ding

s
e

i
n
t
o
i

c
t
s
c
r
nje
I
olle
r
y
t
c
Dire
n
n
Co
de
n
e
p
e
D
Scopes Filters

Providers

Services

Fa

s
e
i
r
cto

Valid
a

tors

20th June 2014, Armin Rdiger 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 Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Workshop Schedule
1. Presentation
2. Live Coding Examples
3. Practicing AngularJS

20th June 2014, Armin Rdiger 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 Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Thanks for your

ttention!

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

20th June 2014, Armin Rdiger Vieweg (@ArminVieweg)


Getting started with superheroic JavaScript library AngularJS

Вам также может понравиться