AngularJS Best Practices

41
Superheroic JavaScript MVW Fram

description

AngularJS Best Practices - Templates -Directives -Businesses logic -$scope -Performance optimization tips AngularJS 1.3

Transcript of AngularJS Best Practices

Page 1: AngularJS Best Practices

Superheroic JavaScript MVW Framework

Page 2: AngularJS Best Practices

Table of contents

•Short about AngularJS

•AngularJS Best practices

•AngularJS 1.3

•AngularJS 2.0

Page 3: AngularJS Best Practices

• Modularity

• Dependency injection

• Two – way binding

• Directives

• Filters

• Templating

• Services

• Testing ready

…. Etc.

Why angular is

powerful

Page 4: AngularJS Best Practices

Best Practices

Page 5: AngularJS Best Practices

Directory structure

Page 6: AngularJS Best Practices
Page 7: AngularJS Best Practices

When angular still loading

• Use ng-bind only for index.html or ng-cloak

• For rest use {{ }}

• When you need to set the src of an image dynamically

use ng-src instead of src with {{ }} template.

• When you need to set the href of an anchor tag

dynamically use ng-href instead of href with {{ }} template.

• Instead of using scope variable as string and using it with style

attribute with {{ }}, use the directive ng-style with object-like

parameters and scope variables as values:

• Use $templateCache to decrease XHR calls (Optimization tips)

Use

Templates

Page 8: AngularJS Best Practices

Now we have powerful templates

Page 9: AngularJS Best Practices

• Name your directives with lowerCamelCase.

• Use the dash-delimited format e.g ng-bind

• Use prefixed names e.g my-customer

• Reserved prefixes ng & ui

• Clean up scope.$on('$destroy', ...)

• use controller when you want to expose an API to other

directives. Otherwise use link.

Directive

Page 10: AngularJS Best Practices

Business logic

• Controllers

oShould not do DOM manipulations

oShould have view behavior

• Services

o Should not contain large DOM manipulations

o Have logic independent of view

o For session-level cache you can

use $cacheFactory. (Optimization tips)

Page 11: AngularJS Best Practices

• Scope is read-only in view

- Scope must refer to the model not to be a

model (model is javascript object)

• Scope is write-only in controller

• $rootScope Unsubscribing watchers manually

Demo with model

Demo without model

$scope

Page 12: AngularJS Best Practices

• Use promises ($q) instead of callbacks.

• Use $resource or RestAngular instead of $http when

working in REST API

• Use angular’s provided services instead of native ,

$timeout,$interval,$window, $document

• AngularJS reserved prefix $, don’t use it when defining a

variables

• In DI order should be angular’s at first, then customs

• Prefer the usage of controllers instead of ng-init

General

Page 13: AngularJS Best Practices

Cool, What about performance ?

Page 14: AngularJS Best Practices

Optimizing angular

• Guaranteed watchers count is 2.000, link to count

watchers

• Limit DOM filters (Angular 1.2.x)

• Each filter runs twice per $digest cycle

• Use $filter provider - $filter('filter')(array, expression, comparator);

• For content what should be rendered only once use

bindonce (Angular 1.2.x) or {{::myVar}} (Angular 1.3.x)

• $timeout & $interval functions has third argument which

will skip $digest loop

Page 15: AngularJS Best Practices

• Don’t write crazy things in $watch callback it will kill your app

• $watch has two comparison modes• referential (default) - quick, shallow comparison

• deep - slow, recurses through objects for a deep comparison; also makes a deep copy of the watched value each time it changes

• $watchCollection function is a sort-of mid-ground between the two $watch() configurations • referential (default) - quick, shallow comparison lika default $watch

• goes one-level deep and performs an additional, shallow reference check of the top level items in the collection

• Avoid deep $watch whenever possible, instead use $watchCollection

Optimizing angular

Page 16: AngularJS Best Practices

• ng-repeat - track by $index

• By default, ng-repeat creates a dom node for each item and

destroys that dom node when the item is removed.

• With track by $index, it will reuse dom nodes.

• Very Bad Practice: $$postDigest

• $$ means private to Angular, so be aware that the interface is not

stable

• Fires a callback after the current $digest cycle completes

• $timeout or $evalAsync ?

Optimizing angular

Page 17: AngularJS Best Practices

Angular 1.3

Page 18: AngularJS Best Practices

Angular 1.3

• Performance (3-4x faster)

• DOM manipulation

• Dirty checking

• Memory usage

• Animations

• Forms

• Flexible validation

• Debouncing

• Error reporting

• Other

• $watchGroup

• templateNamespace

Page 19: AngularJS Best Practices

Performance

Page 20: AngularJS Best Practices

Performance

Page 21: AngularJS Best Practices

Performance

• $httpProvider.useApplyAsync(true);

• one-time binding

Page 22: AngularJS Best Practices

form - ngModel

• ngModel.$validators

• $parsers + $formatters

• ngModel.$asyncValidators

• ngModel.ngModelOptions

Page 23: AngularJS Best Practices

form - ngModel.$validators

Page 24: AngularJS Best Practices

form - ngModel.$validators

Page 25: AngularJS Best Practices

form - ngModel.$asyncValidators

Page 26: AngularJS Best Practices

form - ngModel.ngModelOptions.debounce

Page 27: AngularJS Best Practices

form - ngModel.ngModelOptions-getter/settter

Page 28: AngularJS Best Practices

form - ngModel.ngModelOptions.updateOn

Page 29: AngularJS Best Practices

form - ngMessages

Page 30: AngularJS Best Practices

form - ngMessages

Page 31: AngularJS Best Practices

form - ngMessages

Page 32: AngularJS Best Practices

form - ngMessages

Page 33: AngularJS Best Practices

Other - composable SVG

Page 34: AngularJS Best Practices

Other - allOrNothing

Page 35: AngularJS Best Practices

Other - $watchGroup

Page 36: AngularJS Best Practices

Other - strictDI

ng-europe 2014

video | follow-up post

Page 37: AngularJS Best Practices

Angular 2.0

Page 38: AngularJS Best Practices

Angular 2.0

controllers

DDO

$scope

angular.module

jqLite

generic binding syntax

benchpress

WTF instrumentation

DI query mechanism

ng-europe 2014

video | follow-up post

Page 40: AngularJS Best Practices

Q&A

Page 41: AngularJS Best Practices

Narek Mamikonyan

@nmamikonyan

[email protected]