SlideShare une entreprise Scribd logo
1  sur  30
© Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com
SELA DEVELOPER PRACTICE
MAY 31, 2015 – JUNE 4, 2015
Sebastian Pederiva
Tips for Angular Applications
Senior Consultant
sebastianp@sela.co.il
@spederiva
tiny.cc/SebastianBlog
Starting
Project Structure
Roll your own
Angular Seed
https://github.com/angular/angular- seed
Project Structure…by components
Add Suffix to the file names
Easy to read and understand
Essential for “component type” structure
Help make better automation and testing
product-list.ctrl.js
product-list.tpl.html
product-list.fltr.js
product-list.fctry.js
product-list.srv.js
product-list.const.js
product-list.val.js
Separate to Modules
var corelist = app.module("app.core.list", []);
corelist.controller("app.core.list.listCtrl", ...)
angular.module(“app.common.services")
.service("myApp.common.services.product"
Golden Rules
Controllers should never refer to any DOM
elements
Controllers should call services vs. holding too
much business logic
Avoid $rootScope, try to use services instead
Expression {{}} vs ng-cloak
Minimizing Angular Files
Minimizing process will replace variable names
compromising the dependency injection
someModule.controller('MyController', ['$scope', function($scope) {
$scope.name = "Sebastian";
}]);
someModule.controller('MyController', function ($scope) {
$scope.name = "Sebastian";
});
someModule.controller('MyController', function (a) {
a.name = "Sebastian";
});
One Time Binding
Use double colon ::
https://code.angularjs.org/1.3.15/docs/guide/e
xpression#one-time-binding
For previous versions:
https://github.com/Pasvaz/bindonce
<p id="one-time-binding-example">
One time binding: {{::name}}
</p>
Controller Inheritance
app.controller("ChildCtrl", ["$scope", function ($scope) {
$scope.childText = "childText"
$scope.text = "childText”
}]);
app.controller("ParentCtrl", ["$scope", function ($scope) {
$scope.parentText = "parentText"
$scope.text = "parentText”
}]);
<div ng-controller="ParentCtrl”>
<div ng-controller="ChildCtrl”>
{{parentText}}
<br/>
{{childText}}
<br/>
{{text}}
</div>
</div>
Controller Inheritance
app.controller("ChildCtrl", function ($scope, $controller) {
$controller("ParentCtrl", {$scope: $scope});
$scope.childText = "childText";
$scope.text = "childText";
});
app.controller("ParentCtrl", ["$scope", function ($scope) {
$scope.parentText = "parentText"
$scope.text = "parentText”
}]);
<div ng-controller="ChildCtrl”>
{{parentText}}
<br/>
{{childText}}
<br/>
{{text}}
</div>
Avoid Watchers
$watch occurs when the users clicks on an item,
not when the model changes
Consider using events instead
Minimize use watchers
Angular uses dirty checking to keep track of all
the changes in app
$watch() vs. $watchCollection()
$watch has 3 parameters
Use $watchCollection, it only checks the first
layer of object’s properties
http://www.bennadel.com/blog/2566-scope-
watch-vs-watchcollection-in-angularjs.htm
http://plnkr.co/edit/Pbvo6AqME081rZ2RVKDU?
p=preview
ng-repeat, track when possible
ngRepeat does not allow duplicate items in
arrays
Track by to increase performance
Default tracking
<div ng-repeat=”item in collection track by $id(obj)”>
Own tracking
<div ng-repeat=”item in collection track by item.id”>
https://docs.angularjs.org/api/ng/directive/ngR
epeat#tracking-and-duplicates
ng-repeat
How many times will getPrice function be
called?
Use watch collection to calculate everything
when the controller is first invoked
<tr ng-repeat="item in items">
<td>{{item.name}}</td>
<td>{{item.description}}</td>
<td>{{getPrice(item.id)}}</td>
</tr>
ng-repeat cont
<tr ng-repeat="item in items">
<td>{{item.name}}</td>
<td>{{item.description}}</td>
<td>{{item.price}}</td>
</tr>
$scope.$watchCollection('items', function (newItems) {
for (var i = 0; i < newItems.length; i++) {
newItems[i].price = getPrice(newItems.id);
}
$scope.items = newItems;
});
Avoid ng-repeat for infinite scroll
Memory Leaks
Performance problem
Possible solution:
http://www.williambrownstreet.net/blog/2013/0
7/angularjs-my-solution-to-the-ng-repeat-
performance-problem
Debounce ng-model
Lot of updates?
https://docs.angularjs.org/api/ng/directive/ng
ModelOptions
<input
ng-model="name”
ng-model-options="{ debounce: 500 }” />
Use ng-if instead of ng-show
ng-show will render an element, and use
display:none to hide it
ng-if will actually removes the element from
DOM, and will re-create it, if it’s needed.
Promise you use Promises
Prefer Promises Over Callbacks
Follow The ‘...ing’ Convention
Clear Indication You’re getting a Promise
function gettingUsers(){
return $http()...
}
gettingUsers()
.success()
.error()
Destroying Memory Leaks
Use $scope.$on('$destroy', …)
Remove handlers
Clean up resources which won’t be garbage
collected automatically
module.controller("TestController", function ($scope, $timeout) {
var onTimeout = function () {
$scope.value += 1;
timer = $timeout(onTimeout, 1000);
};
var timer = $timeout(onTimeout, 1000);
$scope.value = 0;
$scope.$on("$destroy", function () {
if (timer) {
$timeout.cancel(timer);
}
});
});
Managing the Time…use moment.js
https://github.com/urish/angular-moment
<span am-time-ago="message.time">
</span>
Use Batarang
Debugging
Pause On Caught Exceptions
Debugging
Attach DOM Breakpoints
Logging
Avoid using console.log in your controllers
Use $log instead
$log can be extended to provide additional
logging capabilities
Forget jQuery Exists
jQuery
-- Use angular.element instead of $() selector --
Search for a jQuery-equivalent instead
» Examples
-- html(), text(), val()
» Ask whether you can use directives instead
Take a look at ui-bootstrap’s directives at
https://github.com/angular-
ui/bootstrap/tree/master/src
Testing
Faster Bug Tracking
Jasmine Given
http://www.airpair.com/angularjs/workshops/u
nit-testing-angularjs
describe("assigning stuff to this", function() {
Given(function() { this.number = 24; });
Given(function() { this.number++; });
When(function() { this.number *= 2; });
Then(function() { expect(this.number).toBe(50) });
});
Links
https://thinkster.io/angulartutorial/a-better-
way-to-learn-angularjs/
http://chieffancypants.github.io/angular-
loading-bar
http://angular-js.in
https://docs.google.com/document/d/1XXMvRe
O8-
Awi1EZXAXS4PzDzdNvV6pGcuaF4Q9821Es/pub
Questions

Contenu connexe

Tendances

SwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsSwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsScott Gardner
 
Making connected apps with BaaS (Droidcon Bangalore 2014)
Making connected apps with BaaS (Droidcon Bangalore 2014)Making connected apps with BaaS (Droidcon Bangalore 2014)
Making connected apps with BaaS (Droidcon Bangalore 2014)Varun Torka
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBChun-Kai Wang
 
Angular 4 with firebase
Angular 4 with firebaseAngular 4 with firebase
Angular 4 with firebaseAnne Bougie
 
Introduction to App Engine Development
Introduction to App Engine DevelopmentIntroduction to App Engine Development
Introduction to App Engine DevelopmentRon Reiter
 
Firebase for Apple Developers
Firebase for Apple DevelopersFirebase for Apple Developers
Firebase for Apple DevelopersPeter Friese
 
Getting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUIGetting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUIScott Gardner
 
Parse: A Mobile Backend as a Service (MBaaS)
Parse: A Mobile Backend as a Service (MBaaS)Parse: A Mobile Backend as a Service (MBaaS)
Parse: A Mobile Backend as a Service (MBaaS)Ville Seppänen
 
 +  = ❤️ (Firebase for Apple Developers) at Swift Leeds
 +  = ❤️ (Firebase for Apple Developers) at Swift Leeds +  = ❤️ (Firebase for Apple Developers) at Swift Leeds
 +  = ❤️ (Firebase for Apple Developers) at Swift LeedsPeter Friese
 
What's new for developers in Dynamics 365 v9: Client API enhancement
What's new for developers in Dynamics 365 v9: Client API enhancementWhat's new for developers in Dynamics 365 v9: Client API enhancement
What's new for developers in Dynamics 365 v9: Client API enhancementKenichiro Nakamura
 
Google App Engine Developer - Day2
Google App Engine Developer - Day2Google App Engine Developer - Day2
Google App Engine Developer - Day2Simon Su
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and AdaptersMohammad Shaker
 
Introduce to PredictionIO
Introduce to PredictionIOIntroduce to PredictionIO
Introduce to PredictionIOWei-Yuan Chang
 
Leveraging Azure Search in Your Application
Leveraging Azure Search in Your ApplicationLeveraging Azure Search in Your Application
Leveraging Azure Search in Your ApplicationJeremy Hutchinson
 

Tendances (20)

SwiftUI and Combine All the Things
SwiftUI and Combine All the ThingsSwiftUI and Combine All the Things
SwiftUI and Combine All the Things
 
Making connected apps with BaaS (Droidcon Bangalore 2014)
Making connected apps with BaaS (Droidcon Bangalore 2014)Making connected apps with BaaS (Droidcon Bangalore 2014)
Making connected apps with BaaS (Droidcon Bangalore 2014)
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Angular 4 with firebase
Angular 4 with firebaseAngular 4 with firebase
Angular 4 with firebase
 
AngularJs
AngularJsAngularJs
AngularJs
 
Introduction to App Engine Development
Introduction to App Engine DevelopmentIntroduction to App Engine Development
Introduction to App Engine Development
 
Firebase for Apple Developers
Firebase for Apple DevelopersFirebase for Apple Developers
Firebase for Apple Developers
 
Getting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUIGetting Started with Combine And SwiftUI
Getting Started with Combine And SwiftUI
 
Parse: A Mobile Backend as a Service (MBaaS)
Parse: A Mobile Backend as a Service (MBaaS)Parse: A Mobile Backend as a Service (MBaaS)
Parse: A Mobile Backend as a Service (MBaaS)
 
 +  = ❤️ (Firebase for Apple Developers) at Swift Leeds
 +  = ❤️ (Firebase for Apple Developers) at Swift Leeds +  = ❤️ (Firebase for Apple Developers) at Swift Leeds
 +  = ❤️ (Firebase for Apple Developers) at Swift Leeds
 
Automated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd DeijlAutomated testing by Richard Olrichs and Wilfred vd Deijl
Automated testing by Richard Olrichs and Wilfred vd Deijl
 
What's new for developers in Dynamics 365 v9: Client API enhancement
What's new for developers in Dynamics 365 v9: Client API enhancementWhat's new for developers in Dynamics 365 v9: Client API enhancement
What's new for developers in Dynamics 365 v9: Client API enhancement
 
Google App Engine Developer - Day2
Google App Engine Developer - Day2Google App Engine Developer - Day2
Google App Engine Developer - Day2
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Wordpress hooks
Wordpress hooksWordpress hooks
Wordpress hooks
 
Introduce to PredictionIO
Introduce to PredictionIOIntroduce to PredictionIO
Introduce to PredictionIO
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Leveraging Azure Search in Your Application
Leveraging Azure Search in Your ApplicationLeveraging Azure Search in Your Application
Leveraging Azure Search in Your Application
 
Firebase ng2 zurich
Firebase ng2 zurichFirebase ng2 zurich
Firebase ng2 zurich
 
#ajn3.lt.marblejenka
#ajn3.lt.marblejenka#ajn3.lt.marblejenka
#ajn3.lt.marblejenka
 

En vedette

An Introduction to Sencha Touch
An Introduction to Sencha TouchAn Introduction to Sencha Touch
An Introduction to Sencha TouchJames Pearce
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React TogetherSebastian Pederiva
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your BusinessBarry Feldman
 

En vedette (7)

ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
HTML5 Graphics
HTML5 GraphicsHTML5 Graphics
HTML5 Graphics
 
Introduction to React
Introduction to ReactIntroduction to React
Introduction to React
 
Sencha Touch
Sencha TouchSencha Touch
Sencha Touch
 
An Introduction to Sencha Touch
An Introduction to Sencha TouchAn Introduction to Sencha Touch
An Introduction to Sencha Touch
 
Combining Angular and React Together
Combining Angular and React TogetherCombining Angular and React Together
Combining Angular and React Together
 
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business32 Ways a Digital Marketing Consultant Can Help Grow Your Business
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
 

Similaire à Tips for Angular Applications

AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014Dariusz Kalbarczyk
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSmurtazahaveliwala
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAERon Reiter
 
Prototyping applications with heroku and elasticsearch
 Prototyping applications with heroku and elasticsearch Prototyping applications with heroku and elasticsearch
Prototyping applications with heroku and elasticsearchprotofy
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015Pushkar Chivate
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar SimovićJS Belgrade
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep diveAxilis
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearchSimobo
 
Summer - The HTML5 Library for Java and Scala
Summer - The HTML5 Library for Java and ScalaSummer - The HTML5 Library for Java and Scala
Summer - The HTML5 Library for Java and Scalarostislav
 
ANGULARJS.pdf
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdfArthyR3
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto UniversitySC5.io
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 
Ako prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s ElasticsearchAko prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s Elasticsearchbart-sk
 

Similaire à Tips for Angular Applications (20)

AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014AngularJS Mobile Warsaw 20-10-2014
AngularJS Mobile Warsaw 20-10-2014
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
Prototyping applications with heroku and elasticsearch
 Prototyping applications with heroku and elasticsearch Prototyping applications with heroku and elasticsearch
Prototyping applications with heroku and elasticsearch
 
Django
DjangoDjango
Django
 
SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015SharePoint Saturday Atlanta 2015
SharePoint Saturday Atlanta 2015
 
Angular js
Angular jsAngular js
Angular js
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearch
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Summer - The HTML5 Library for Java and Scala
Summer - The HTML5 Library for Java and ScalaSummer - The HTML5 Library for Java and Scala
Summer - The HTML5 Library for Java and Scala
 
ANGULARJS.pdf
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdf
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Why ruby on rails
Why ruby on railsWhy ruby on rails
Why ruby on rails
 
Angular.js Primer in Aalto University
Angular.js Primer in Aalto UniversityAngular.js Primer in Aalto University
Angular.js Primer in Aalto University
 
Meteor Day Talk
Meteor Day TalkMeteor Day Talk
Meteor Day Talk
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
Ako prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s ElasticsearchAko prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s Elasticsearch
 

Dernier

Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Balliameghakumariji156
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...kumargunjan9515
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...gajnagarg
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsMonica Sydney
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...meghakumariji156
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsMonica Sydney
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样ayvbos
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirtrahman018755
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge GraphsEleniIlkou
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理F
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查ydyuyu
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoilmeghakumariji156
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdfMatthew Sinclair
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdfMatthew Sinclair
 
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsMira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsPriya Reddy
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasDigicorns Technologies
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制pxcywzqs
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdfMatthew Sinclair
 

Dernier (20)

Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime BalliaBallia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
Ballia Escorts Service Girl ^ 9332606886, WhatsApp Anytime Ballia
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
 
Call girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girlsCall girls Service in Ajman 0505086370 Ajman call girls
Call girls Service in Ajman 0505086370 Ajman call girls
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call GirlsMira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
Mira Road Housewife Call Girls 07506202331, Nalasopara Call Girls
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 

Tips for Angular Applications

  • 1. © Copyright SELA software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com SELA DEVELOPER PRACTICE MAY 31, 2015 – JUNE 4, 2015 Sebastian Pederiva Tips for Angular Applications Senior Consultant sebastianp@sela.co.il @spederiva tiny.cc/SebastianBlog
  • 3. Project Structure Roll your own Angular Seed https://github.com/angular/angular- seed
  • 5. Add Suffix to the file names Easy to read and understand Essential for “component type” structure Help make better automation and testing product-list.ctrl.js product-list.tpl.html product-list.fltr.js product-list.fctry.js product-list.srv.js product-list.const.js product-list.val.js
  • 6. Separate to Modules var corelist = app.module("app.core.list", []); corelist.controller("app.core.list.listCtrl", ...) angular.module(“app.common.services") .service("myApp.common.services.product"
  • 7. Golden Rules Controllers should never refer to any DOM elements Controllers should call services vs. holding too much business logic Avoid $rootScope, try to use services instead Expression {{}} vs ng-cloak
  • 8. Minimizing Angular Files Minimizing process will replace variable names compromising the dependency injection someModule.controller('MyController', ['$scope', function($scope) { $scope.name = "Sebastian"; }]); someModule.controller('MyController', function ($scope) { $scope.name = "Sebastian"; }); someModule.controller('MyController', function (a) { a.name = "Sebastian"; });
  • 9. One Time Binding Use double colon :: https://code.angularjs.org/1.3.15/docs/guide/e xpression#one-time-binding For previous versions: https://github.com/Pasvaz/bindonce <p id="one-time-binding-example"> One time binding: {{::name}} </p>
  • 10. Controller Inheritance app.controller("ChildCtrl", ["$scope", function ($scope) { $scope.childText = "childText" $scope.text = "childText” }]); app.controller("ParentCtrl", ["$scope", function ($scope) { $scope.parentText = "parentText" $scope.text = "parentText” }]); <div ng-controller="ParentCtrl”> <div ng-controller="ChildCtrl”> {{parentText}} <br/> {{childText}} <br/> {{text}} </div> </div>
  • 11. Controller Inheritance app.controller("ChildCtrl", function ($scope, $controller) { $controller("ParentCtrl", {$scope: $scope}); $scope.childText = "childText"; $scope.text = "childText"; }); app.controller("ParentCtrl", ["$scope", function ($scope) { $scope.parentText = "parentText" $scope.text = "parentText” }]); <div ng-controller="ChildCtrl”> {{parentText}} <br/> {{childText}} <br/> {{text}} </div>
  • 12. Avoid Watchers $watch occurs when the users clicks on an item, not when the model changes Consider using events instead Minimize use watchers Angular uses dirty checking to keep track of all the changes in app
  • 13. $watch() vs. $watchCollection() $watch has 3 parameters Use $watchCollection, it only checks the first layer of object’s properties http://www.bennadel.com/blog/2566-scope- watch-vs-watchcollection-in-angularjs.htm http://plnkr.co/edit/Pbvo6AqME081rZ2RVKDU? p=preview
  • 14. ng-repeat, track when possible ngRepeat does not allow duplicate items in arrays Track by to increase performance Default tracking <div ng-repeat=”item in collection track by $id(obj)”> Own tracking <div ng-repeat=”item in collection track by item.id”> https://docs.angularjs.org/api/ng/directive/ngR epeat#tracking-and-duplicates
  • 15. ng-repeat How many times will getPrice function be called? Use watch collection to calculate everything when the controller is first invoked <tr ng-repeat="item in items"> <td>{{item.name}}</td> <td>{{item.description}}</td> <td>{{getPrice(item.id)}}</td> </tr>
  • 16. ng-repeat cont <tr ng-repeat="item in items"> <td>{{item.name}}</td> <td>{{item.description}}</td> <td>{{item.price}}</td> </tr> $scope.$watchCollection('items', function (newItems) { for (var i = 0; i < newItems.length; i++) { newItems[i].price = getPrice(newItems.id); } $scope.items = newItems; });
  • 17. Avoid ng-repeat for infinite scroll Memory Leaks Performance problem Possible solution: http://www.williambrownstreet.net/blog/2013/0 7/angularjs-my-solution-to-the-ng-repeat- performance-problem
  • 18. Debounce ng-model Lot of updates? https://docs.angularjs.org/api/ng/directive/ng ModelOptions <input ng-model="name” ng-model-options="{ debounce: 500 }” />
  • 19. Use ng-if instead of ng-show ng-show will render an element, and use display:none to hide it ng-if will actually removes the element from DOM, and will re-create it, if it’s needed.
  • 20. Promise you use Promises Prefer Promises Over Callbacks Follow The ‘...ing’ Convention Clear Indication You’re getting a Promise function gettingUsers(){ return $http()... } gettingUsers() .success() .error()
  • 21. Destroying Memory Leaks Use $scope.$on('$destroy', …) Remove handlers Clean up resources which won’t be garbage collected automatically module.controller("TestController", function ($scope, $timeout) { var onTimeout = function () { $scope.value += 1; timer = $timeout(onTimeout, 1000); }; var timer = $timeout(onTimeout, 1000); $scope.value = 0; $scope.$on("$destroy", function () { if (timer) { $timeout.cancel(timer); } }); });
  • 22. Managing the Time…use moment.js https://github.com/urish/angular-moment <span am-time-ago="message.time"> </span>
  • 26. Logging Avoid using console.log in your controllers Use $log instead $log can be extended to provide additional logging capabilities
  • 27. Forget jQuery Exists jQuery -- Use angular.element instead of $() selector -- Search for a jQuery-equivalent instead » Examples -- html(), text(), val() » Ask whether you can use directives instead Take a look at ui-bootstrap’s directives at https://github.com/angular- ui/bootstrap/tree/master/src
  • 28. Testing Faster Bug Tracking Jasmine Given http://www.airpair.com/angularjs/workshops/u nit-testing-angularjs describe("assigning stuff to this", function() { Given(function() { this.number = 24; }); Given(function() { this.number++; }); When(function() { this.number *= 2; }); Then(function() { expect(this.number).toBe(50) }); });

Notes de l'éditeur

  1. Faster Unit Testing Gives Context Easier To Remove Parts Of The App
  2. "How do I pass things between controllers?" --- ...probably means that you are doing things wrong The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading
  3. Angular 1.3 added :: Angular will wait for a value to stabilize after it’s first series of digest cycles, and will use that value to render the DOM element. After that, Angular will remove the watcher forgetting about that binding
  4. Minimize use watchers Angular uses dirty checking to keep track of all the changes in app through every watcher to check if they need to be updated If one of the watcher is relied upon by another watcher, Angular would have to re-run the digest cycle again
  5. Third parameters is deep eval $watchCollection acts almost like $watch with a 3rd parameter except it only checks the first layer of object’s properties
  6. ngRepeat does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements. ngRepeat will not have to rebuild the DOM elements for items it has already rendered
  7. Because ng-repeat uses watchers ng-repeat creates bad performance and AngularJS is getting slower with more than 2000-2500 two-way bindings to watch
  8. DOM traversal is also problematic Separation of Concerns Overkiller
  9. Using TDD for Better Engineering