30
ngularJS features by Alexey Migutsky …and related gotchas

5 angularjs features

Embed Size (px)

DESCRIPTION

5 AngularJS features with

Citation preview

Page 1: 5 angularjs features

ngularJS features

by Alexey Migutsky

all links are on the last slide

…and related gotchas

Page 2: 5 angularjs features

1. SCOPEIn scope we trust [1]

Page 3: 5 angularjs features

Scope is created

$rootScope (always there)

ng-repeat(for each iteration, inherits)

ng-include(inherits)

ng-switch(inherits)

ng-if(when creating element)

ng-controller (inherits)

ng-model(inherits, thro implicit controller)

ng-view(inherits, thro controller)

directive with scope:true(inherits)

directive with transclude:true(inherits, sibling)

directive with scope: {} (does not inherit)

Page 4: 5 angularjs features

Scope is not created

directive with scope: false (default value)

All directives, not mentioned on the previous slide

Page 5: 5 angularjs features

Angular always tracks child-parent relationships, even without inheritance

but $parent referencing is bad!

Page 6: 5 angularjs features

There can be only 1 scope per 1 element [2]

Directives are not mixins Isolated scope can isolate non-isolated scopes on the same element

Page 7: 5 angularjs features

Transcluded scope is sibling to directive’s scope

$parentdirective’s scope

transcluded $scope

Page 8: 5 angularjs features

2. DIGESTINGRun, $digest, run! [3]

Page 9: 5 angularjs features
Page 10: 5 angularjs features

Do not use $digest, use $apply in directives.

$digest is a single run, not the full cycle

Page 11: 5 angularjs features

$watch expression is called in $scope’s context!

In the expression this will refer to the $scope!Do not use $watch with property getters without binding.

Page 12: 5 angularjs features

$digest event propagates inside isolated scopes!

The change in parent scope will trigger isolated watchers

Page 13: 5 angularjs features

Use events to handle ‘model changes’ sparingly [4]

Use $watch instead.Digest cycle is fast…

…until you write bloated $watch exressions.

Watchtower [5] comes to the rescue!

Page 14: 5 angularjs features

3. DATABINDINGMy name is Bind, Data Bind [6]

Page 15: 5 angularjs features

Expressions are JavaScript-like code

In case of emergency use $eval() and not eval()

Page 16: 5 angularjs features

Expressions have no flow control

Great decision for templates!

Page 17: 5 angularjs features

Expressions are evaluated against current scope.

Remember the $watch gotcha?

Page 18: 5 angularjs features

Expressions can have function call reference

You can specify parameters to be passed upon the call.“someMethodCall( param1, param2 )”

Not very common, is it?

It should be called like this in JS:someMethodCall( { param1 : ‘value’,

param2: ‘value2’})

Page 19: 5 angularjs features

4. DEPENDENCY INJECTION

Injections are the best thing ever invented ... [7]

Page 20: 5 angularjs features

The only orthodoxal way to establish relationships between components is DI.

Page 21: 5 angularjs features
Page 22: 5 angularjs features

Remember the ‘minification problem’[8]

Use array-based declarations.Or just use ngmin [9].

Page 23: 5 angularjs features

AngularJS ‘runtime’ is an IoC container.

Read it like this: you should wrap 3rd party components, which are not integrated with angular, into directives/services.

You can’t inject (use) them as is!

Page 24: 5 angularjs features

5. MODULARITYTrue independence and freedom can only exist in doing what's right. [10]

Page 25: 5 angularjs features

Always use angular.module syntax

It is good for testing.It is good for decoupling.

It helps manage dependencies.

Page 26: 5 angularjs features

Prefer module-per-feature approach [11]

It is good for testing.It is good for refactoring and maintenance.

Page 27: 5 angularjs features

Using Angular with module loaders can be tricky

Angular ‘modules’ has a different nature.Need some handcrafting to make modules and ‘modules’ play nicely.

Page 28: 5 angularjs features

Keep services/directives/modules as independent as possible. This is #1 priority.

Use isolated scope for main directives.Keep shared state in services.Keep constructors in factories.

Page 29: 5 angularjs features