KEMBAR78
Optimizing AngularJS Application | PDF
Md. 
Ziaul 
Haq 
Sr. 
JavaScript 
Developer 
oDesk 
Inc. 
@jquerygeek
” What 
&& 
Why
” Identify 
the 
problems 
” The 
solution 
with 
best 
practices 
” Few 
more 
best 
practices
” Network 
related 
issue 
” Unexpected 
characters 
” Slow 
DOM 
” Slow 
watcher
” Minify 
your 
codes 
§ Use 
Uglify
” Write 
minify 
safe 
code 
§ Use 
Dependency 
Annotation
” Without 
Dependency 
Annotation 
myApp.service(’myService’, 
function($http){ 
console.log($http); 
});
” Without 
Dependency 
Annotation 
myApp.service("myService", 
function(a){ 
console.log(a) 
});
” With 
Dependency 
Annotation 
myApp.service(’myService’, 
[ā€˜$http’, function($http){ 
console.log($http); 
}]);
” Use 
ā€˜ng-­‐annotate’ 
§ https://github.com/olov/ng-­‐annotate
” Lot 
of 
network 
request 
§ Mostly 
for 
template 
files
” Cache 
the 
templates 
§ Use 
$templateCache 
service
myApp.run([ā€˜$templateCache’, 
function($templateCache) { 
$templateCache.put('tpls/ 
hello.html', '<div class='hello 
'>Hello, {{world}}!</div>’); 
}]);
” For 
grunt 
§ https://github.com/karlgoldstein/grunt-­‐html2js 
” For 
gulp 
§ https://github.com/fraserxu/gulp-­‐html2js
” Template 
tag 
{{ 
}} 
in 
landing 
page
— Hide 
the 
body/some 
element, 
till 
bootstrap 
angular 
— Use 
ng-­‐cloak
<Style> 
[ng-cloak] { 
display: none; 
} 
</style> 
<body ng-cloak>
ā–Ŗ Hide 
individual 
expressions 
ā–Ŗ Use 
ng-­‐bind 
<span ng-bind=ā€data.valueā€> 
</span>
” https://builtwith.angularjs.org/
” Repeating 
manipulation
” Optimize 
ng-­‐repeat 
§ Use 
the 
track 
by 
syntax 
<li ng-repeat=ā€ 
item in data.items 
track by item.id ā€> 
</li>
” Use 
bind-­‐once 
<li ng-repeat=ā€ 
item in ::data.items"> 
{{::item.title}} 
</li>
” Filter 
in 
controller, 
not 
in 
ā€˜ng-­‐repeat’
” Do 
you 
need 
$watch? 
” Use 
unWatch 
” Keep 
$watch 
smaller 
and 
focused 
” Avoid 
loop 
in 
$watch
” Avoid 
deep 
comparison 
$scope.user = { 
id: grrey, 
name: Gandaf Grey, 
email: ggreay@gmail.com 
} 
$scope.$watch('user', function(newUser) { 
//... 
}, true);
” Keep 
filter 
simple 
with 
single 
task 
” Use 
bind-­‐once 
” Keep 
less 
item 
in 
ng-­‐repeat
” Bootstrap 
Angular 
app 
§ Angular-­‐seed 
https://github.com/angular/angular-­‐seed 
§ Angular-­‐fun 
https://github.com/CaryLandholt/ 
AngularFun
” Separate 
the 
business 
logic 
§ Controller 
ā–Ŗ Manipulate 
DOM 
in 
Controller 
Directives 
ā–Ŗ Commonly 
used 
functions 
as 
service. 
ā–Ŗ Use 
$emit, 
$broadcast 
or 
$on 
to 
communicate 
with 
other 
controller.
” Separate 
the 
business 
logic 
§ Service 
ā–Ŗ Should 
not 
refer 
DOM 
ā–Ŗ All 
business 
login 
in 
service
§ Directive 
ā–Ŗ DOM 
manipulations 
must 
in 
directives 
ā–Ŗ Use 
scope 
instead 
of 
$scope 
in 
link 
ā–Ŗ Isolated 
scope 
for 
reusable 
components. 
ā–Ŗ Custom 
prefix 
to 
avoid 
conflict 
ā–Ŗ Don’t 
use 
ā€˜ng’ 
and 
ā€˜ui’ 
as 
prefix.
§ Use 
of 
Directive 
ā–Ŗ Use 
ā€˜data’ 
prefix 
for 
valid 
HTML 
" 
<div data-my-directive> 
</div>
” Choose 
the 
better 
one 
§ Use 
promises 
($q) 
instead 
of 
old 
callbacks. 
§ Use 
ngIf 
instead 
of 
ngShow 
& 
ngHide
” Choose 
the 
better 
one 
§ Use 
ng-­‐src 
instead 
of 
src=ā€œ{{ 
}}ā€ 
§ Use 
ng-­‐href 
instead 
of 
href=ā€œ{{ 
}}ā€
” Avoid 
old/jQuery 
habit 
§ $timeout 
instead 
of 
setTimeout 
§ $interval 
instead 
of 
setInterval 
§ $window 
instead 
of 
window 
§ $document 
instead 
of 
document 
§ $http 
instead 
of 
$.ajax
” https://docs.angularjs.org/ 
” https://github.com/mgechev/angularjs-­‐style-­‐ 
guide 
” ng-­‐book
Thanks, 
all. 
J 
Drop 
me 
a 
line 
if 
more 
question 
jquerygeek@elance-­‐odesk.com

Optimizing AngularJS Application