Escolar Documentos
Profissional Documentos
Cultura Documentos
Brad Green
#DV13-NGU
Brian Ford
Miko Hevery
Igor Minar
@angularjs
#DV13-NGU
@angularjs
Setup: Template
< htm lng-app= 'coolApp'>
< body>
@angularjs
Setup: JS
#DV13-NGU
@angularjs
Principles
Boilerplate
D.R.Y.
#DV13-NGU
Structure
Testability
@angularjs
Structure: Model-View-Controller
<div>
<span>
<ul>
<li>
View
(DOM)
Observes
Notifies
RAM
Model
(JS Objects)
Manages
Controller
(JS Classes)
#DV13-NGU
@angularjs
Model:
View:
Controller:
m yApp.controller('PersonController', function () {
this.m odel= { nam e: 'M isko' };
});
#DV13-NGU
@angularjs
Model:
View:
Controller:
#DV13-NGU
@angularjs
hello.html
{{ greeter.greeting }}
// 1-w ay
#DV13-NGU
@angularjs
@angularjs
Feature: Directives
< div n g -rep eat= 'item in cart.item s'>
< span n g -b in d = 'item .nam e'> < /span>
< button n g -click= 'cart.delete($index)'>
D elete
< /button>
< /div>
#DV13-NGU
@angularjs
@angularjs
@angularjs
Preferred usage
m yApp.service('foo', function() {
...
});
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
View Templates
Directives
Markup {{ }}
Form Validation
Filters
#DV13-NGU
@angularjs
Filters
< p> {{ invoice.total| currency }}< /p>
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
Back to Controllers
Thus far:
Expose model to view
Expose functions to handle user interaction
Update model to change the view
But also:
Instantiated once per use in your template
Can request $scope
#DV13-NGU
@angularjs
$scope
You can request $scope in controllers
m yApp.controller('G reetCtrl', function ($scope) {
$scope.greeter = { greeting: 'M isko'};
});
Use in template
< p ng-controller= 'G reetC trl'> {{greeter.greeting}}< /p>
#DV13-NGU
@angularjs
$scope
Context for expression evaluation
Hierarchical, mirroring your DOM structure
Prototypal inheritance = global read, local write
#DV13-NGU
@angularjs
$scope
$watch(expression, callback): Executes a callback
whenever a given expression changes
$apply() : synchronize data binding from outside
Angular's event loop
#DV13-NGU
@angularjs
Expressions
{{ The stuff inside your markup }}
ng-bind='and inside directives'
Like JavaScript, but eval'd against your $scope
No complex logic like loops or throws
Though terinary operator is supported
#DV13-NGU
@angularjs
Expression Examples
person.name
shoppingCart.total()
cart.total() - user.discountRate
#DV13-NGU
@angularjs
Talking to servers
$h ttp : HTTP server communication
$resou rce: RESTful server communication
Notable other options:
Restangular
AngularFire
BreezeJS
#DV13-NGU
@angularjs
$http
this.som eD ata = $http.get('/som eU rl');
#DV13-NGU
@angularjs
$http
$http.get('/som eU rl').
success(function(data, status, headers, confi
g) {
// called asynchronously
}).
error(function(data, status, headers, confi
g) {
// called if an error occurs, also async
});
#DV13-NGU
@angularjs
Promises
$http.post('/user/', user).th en (
function() {
alert('Successfully created user!');
}, function() {
alert('Error creating user...');
}
);
#DV13-NGU
@angularjs
Routes
Left Nav
Header
#DV13-NGU
Content View
@angularjs
Routes
< n g -view > directive as content placeholder
$rou teP rovid er to configure:
Controller
Template
URL
#DV13-NGU
@angularjs
Routes
m yApp.confi
g (function($routeProvider) {
$routeProvider.w hen('/inbox', {
tem plateU rl: 'inbox.htm l',
controller: 'InboxCtrl',
controllerAs: 'inbox'
});
});
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
Resources
Tools
Batarang
#DV13-NGU
UI Components
Libraries
Books
Bootstrap
@angularjs
Learning Resources
docs.angularjs.org
/tutorial
#DV13-NGU
egghead.io
thinkster.io
pluralsight.com
@angularjs
Questions?
#DV13-NGU
@angularjs
Internals of AngularJS
(How it all works)
Miko Hevery
#DV13-NGU
@angularjs
Data Binding
#DV13-NGU
@angularjs
Data Binding
<div>
Model
Drives
<span>
<ul>
<li>
<li>
<li>
<div>
Model
Depends on
<span>
<ul>
<li>
<li>
<li>
#DV13-NGU
@angularjs
Model is a Plain-Old-JavaScript-Object
Detect value changes
Easily describe value in the model
Encapsulate DOM access
#DV13-NGU
@angularjs
Scope
#DV13-NGU
@angularjs
Scope
User = function () {
this.fullName = 'Miko Hevery';
this.username = 'mhevery';
}
scope = {};
scope.user = new User();
{{user.fullName}}
{{user.username}}
#DV13-NGU
@angularjs
Scope
{{name}}
<ul>
<li ng-repeat="name in names">
{{name}}
</li>
</ul>
#DV13-NGU
@angularjs
Scope
parent = {};
child = inherit(parent);
parent.name = 'parent';
expect(child.name).toEqual('parent');
child.name = 'child');
expect(child.name).toEqual('child');
expect(parent.name).toEqual('parent');
#DV13-NGU
@angularjs
Scope
function inherit(parent) {
function C() {}
C.prototype = parent;
return new C();
}
parent = {};
child = inherit(parent);
expect(child.__proto__).toBe(parent);
#DV13-NGU
@angularjs
Scope
{{name}}
<ul>
<li ng-repeat="name in names">
{{name}}
</li>
</ul>
#DV13-NGU
@angularjs
Watchers
#DV13-NGU
@angularjs
Watchers
{{name}}
scope.name = 'Miko Hevery';
scope.$watch('name', function(value) {
element.text(value);
});
#DV13-NGU
@angularjs
Digest Cycle
#DV13-NGU
@angularjs
Digest Cycle
scope.$watch('name', reactionFn);
#DV13-NGU
var lastValue;
function dirtyCheck() {
var value = scope.name;
if (lastValue != value)
reactionFn(value);
lastValue = value;
}
@angularjs
Digest Cycle
scope.$watch('count', function(value) {
alert(value);
});
scope.$watch('name', function(value) {
scope.count++;
});
#DV13-NGU
@angularjs
Digest Cycle
#DV13-NGU
@angularjs
Digest Cycle
#DV13-NGU
@angularjs
@angularjs
#DV13-NGU
asyncApi()
yourApplicationBehavior()
ng.$apply()
nativeCode(event)
@angularjs
Expressions
#DV13-NGU
@angularjs
Expressions
{{user.firstName}}
eval('with(scope){return
user.firstName;}');
if (scope.user) {
return scope.user.firstName;
}
#DV13-NGU
@angularjs
Expressions
#DV13-NGU
@angularjs
Expressions
#DV13-NGU
@angularjs
Directive
#DV13-NGU
@angularjs
Directive
<ul>
<li ng-repeat="user in users">
{{user.name}}
</li>
</ul>
#DV13-NGU
@angularjs
Directive
scope.$watch('name', function(value) {
element.text(value);
});
<span ng-bind="name"></span>
#DV13-NGU
@angularjs
Compiler
#DV13-NGU
@angularjs
Compiler
<div ng-controller="MyCtrl as c">
<div ng-repeat="name in c.names">
{{name}}</div>
</div>
@angularjs
Dependency Injection
#DV13-NGU
@angularjs
Dependency Injection
function Door() {}
function House(door) {}
function App(house) {}
// implied
app = new App(new House(new Door)));
#DV13-NGU
@angularjs
Dependency Injection
#DV13-NGU
@angularjs
Dependency Injection
function Door() {}
function House(door) {}
House.$inject = ['door'];
function App(house) {}
App.$inject = ['house'];
#DV13-NGU
@angularjs
Dependency Injection
describe('something', function() {
function test($rootScope){}
test.$inject = ['$rootScope'];
it('should ...', test);
});
#DV13-NGU
@angularjs
Dependency Injection
function House(door) {}
House.$inject =
creazyRegExp(House.toString());
#DV13-NGU
@angularjs
Dependency Injection
function Door(){}
function House(door) {}
injector = new Injector(...);
house = injector.instantiate(House);
#DV13-NGU
@angularjs
Dependency Injection
function instantiate(Type) {
var args = Type.$inject.map(instantiate);
return new Type.call(args);
}
#DV13-NGU
@angularjs
Dependency Injection
#DV13-NGU
@angularjs
Routing
#DV13-NGU
@angularjs
Routing
Deep linking
Links work like links
Support HTML5 pushstate
URL rewriting
#DV13-NGU
@angularjs
Routing
<a href="/chapter/{{id}}">
http://server.com/chapter/123
http://server.com/#!/chapter/123
@angularjs
REST
#DV13-NGU
@angularjs
REST
#DV13-NGU
@angularjs
REST
Chapter = $resource('/chapter/:chapterId');
var chapters = Chapter.query();
<div ng-repeat="chapter in chapters">
{{chapter.name}}
</div>
#DV13-NGU
@angularjs
Questions?
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
Animations in Angular
Animations are a natural
extension of CSS3 animations
and AngularJS directives
#DV13-NGU
@angularjs
Animations in Angular
#DV13-NGU
@angularjs
Animations
// HTML
<div ng-repeat="item in list" class="item">{{item}}</div>
//CSS
.item.ng-enter, .item.ng-move {
.item.ng-leave {
}
@keyframes my_animation {
.item.ng-enter.ng-enter-active,
from { opacity:1; }
.item.ng-move.ng-move-active {
to { opacity:0; }
opacity: 1;
}
#DV13-NGU
@angularjs
Animation Hooks
http://docs.angularjs.org/guide/animations
#DV13-NGU
@angularjs
Animation in Action
Example App: https://github.com/matsko/ngconf-animations-demo
#DV13-NGU
@angularjs
ngIf
ngSwipeLeft/Right
https://github.com/btford/brian-talks-about-animations
#DV13-NGU
@angularjs
Animation in Action
Example: Slideshow
ngView
https://github.com/btford/brian-talks-about-animations
#DV13-NGU
@angularjs
Animation in Action
Example: Notifications
ngRepeat
https://github.com/btford/brian-talks-about-animations
#DV13-NGU
@angularjs
Be tasteful
use sparingly
same functionality, but better affordances
#DV13-NGU
@angularjs
(ng-repeat)
ng-repeat is stable
Database data needs to be refreshed
<div ng-repeater="o in objs track by o.id">
...
</div>
#DV13-NGU
@angularjs
"Controller As"
Before:
<div ng-controller="UserController">
<p>{{name}}</p>
<div ng-if="someCondition">
<input ng-model="name">
</div>
</div>
#DV13-NGU
@angularjs
"Controller As"
Unambiguously bind to models:
<div ng-controller="UserController as
user">
<p>{{user.name}}</p>
<div ng-if="someCondition">
<input ng-model="user.name">
</div>
</div>
#DV13-NGU
@angularjs
Security Enhancements
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
Before:
After:
Uncaught Error: [di-nomod] Module 'ngResource' is not
available! You either misspelled the module name or
forgot to load it.
http://docs.angularjs.org/error/di-nomod?a=ngResource
In production:
Uncaught Error: [di-nomod] ngResource
http://docs.angularjs.org/error/di-nomod?a=ngResource
#DV13-NGU
@angularjs
#DV13-NGU
@angularjs
More Angular!
Between 1.0.0 and 1.2.0 ...
1,575 SHAs/PRs
2,850 Issues closed
Direc
tory
*
#DV13-NGU
LOC
Added
LOC
Remo
ved
101,489
36,938
LOC
Total
@angularjs
#DV13-NGU
What
W When
h
er
e
Advanced
AngularJS
Workshop
B
O
F
1
Unit testing
JavaScript /
CoffeeScrip
t / Dart
code with
R Tue
o 17:25 o 17:55
m
8
Mon
13:30 16:30
@angularjs
Questions?
#DV13-NGU
@angularjs