SlideShare a Scribd company logo
Supercharging
AngularJS
lessons learnt, experiences, best practices…
for building modern AngularJS 1.x apps
@IMPACTdigitaldk
AngularJS and
Angular
Filip
#weAreHiring #Denmark
https://developers.google.com/experts/people/filip-bruun-bech-larsen
lets talk about
AngularJS
Why best practices
• for better apps
• less errors
• better performance
• better team-work
• more fun
• to get closer to ng2
Change your vocabulary
• Components (element-selector + template)
• Directives (attribute-selector + never template)
think of your app as a
tree of components
A tree of components
my-app
app-navigationcontact-list
contact-item
toggle-favorite
contact-item
toggle-favorite
contact-item
toggle-favorite
AngularJS best practices
Think in Smart/Dumb
stateful
stateless
Data down and events up
AngularJS best practices
Business logic and data
live in services
when you are calling $http from a component - you are doing it
wrong
go for consistency in
data-calls
so probably async, if any of it is…
Treat directives like
components
don’t use link(), don’t use replace
$scope
What is it with scope
inheritance/isolation and performance
don’t use scope/$scope
go for ctrlAs-syntax (default on components)
if you really need an event-system, scope can be ok
when things are
slow…you are probably doing it wrong
don’t ever $watch
$doCheck() does it better, but you should probably
refactor
$apply is the root of all
evil
ng-* event-listeners, $http, $q.resolve, $q.reject, $timeout,
default to ::bindings
upgrade only as needed
or maybe just put the data in the template-string directly
AngularJS best practices
One-time
is not
one-way
one-way bindings are
a special input-binding
its almost like two way bindings (=), except for when you
change the reference from inside the component.
http://codepen.io/filipbech/pen/pNLPdN
{{avoidThese}}
use ng-bind so the expression is cached
and don’t use ng-include
subcomponents are almost twice as fast
Remember to do clean-up
remove event/message listeners and pointers in
$onDestroy
http://codepen.io/filipbech/pen/gLegyx?editors=0010
Watch out for filters in your
template
they are run more often than you think
http://codepen.io/filipbech/pen/qqomQQ?editors=0010
ng-repeat
use trackBy and ::binding
Consider typescript
for ES6 and for the types
Consider Observables
with RxJS 5
what about modules
angular modules, es6-modules, AMD etc…
Manage your state
consistently
consider redux, observables or roll your own system
files, methods, variables, etc.
Avoid ng and $ in your own names
Commit to a naming-scheme
DI on separate lines
so you’ll have pretty diffs
Use the newest
angularJS-release
AngularJS in
production
obviously concat, minify, gzip, etc…
but also: use provided .min bundle,
disable debug info, disable comment/class directive
https://docs.angularjs.org/guide/production
so what do you think…
questions, thoughts, comments?
Thank you
I’ll tweet a link to the slides in a minute
@IMPACTdigitaldk
@filipbech
AngularJS best practices

More Related Content

AngularJS best practices