angular deep directive

32
Angular-Deep-Directive From Nutshell To Awesome By HenryTao – http://henrytao.me - https://mysquar.com

Upload: henry-tao

Post on 14-Jul-2015

275 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Angular Deep Directive

Angular-Deep-DirectiveFrom Nutshell To Awesome

By HenryTao – http://henrytao.me - https://mysquar.com

Page 2: Angular Deep Directive

Technology lover.

Full-stack Engineer at MySQUAR.

Community project – angularify.org.

(angularify, angular-offline, angular-debone,

angular-emojiuni, angular-detector, angular-cookies…)

By HenryTao – http://henrytao.me - https://mysquar.com

[email protected]

github.com/henrytao-me

angularify.org

2

Page 3: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com 3

What can you take away after this?

scope (=, $, &)

transclude (dynamic content)

link function VS compile function

nested directive

Page 4: Angular Deep Directive

Agenda

AngularJS Directive overview and tips.

Advance Directive.

Time to Hack.

Resources & References.

By HenryTao – http://henrytao.me - https://mysquar.com 4

Page 5: Angular Deep Directive

// declare

<div mysquar-directive></div>

// result

<div mysquar-directive>Welcome to MySQUAR</div>

// javascript code

angular.module('myquarApp',[

]).directive('mysquarDirective', function() {

return {

link: function($scope, $element, $attrs) {

$element.html('Welcome to MySQUAR’);

}

};

});

By HenryTao – http://henrytao.me - https://mysquar.com

MySQUAR Directive

5

Page 6: Angular Deep Directive

Directive is a function

that run on particular

DOM element

By HenryTao – http://henrytao.me - https://mysquar.com 6

Page 7: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

DOM $compile $link DOM’

restrict

priority

terminal

template

templateUrl

replace

controller

require

scope

transclude

link

compile

pre / post

7

Page 8: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

RESTRICT

E (element) A (attribute) C (class) M (comment)

Don’t recommend using comment

Using attribute if want to support IE

Can use prerender server for IE

8

Page 9: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

PRIORITY

Default priority for custom directive is 0

Priority of ngRepeat is 1000

9

Name ordering is last option

<div first second></div>

<div second first></div>

High priority run first

Page 10: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

TERMINAL

<div first second></div>

<div second first></div>

10

app.directive('first', function() {

return {

priority: 1,

link: function(scope, element, attrs) {

// never execute

}

};

}); app.directive('second', function() {

return {

priority: 2,

terminal: true,

link: function(scope, element, attrs) {

// execute & stop

}

};

});

Page 11: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

TEMPLATE

TEMPLATEURL

REPLACE

CONTROLLER

REQUIRE

11

Page 12: Angular Deep Directive

angular.module(‘app', []).directive('powerSwitch', function() {

return {

controller: function($scope, $element, $attrs) {

$scope.state = 'on';

this.getState = function() { return $scope.state;};

}

};

}).directive('lightbulb', function() {

return {

require: '^powerSwitch',

link: function(scope, element, attrs, powerSwitchCtrl) {

scope.$watch('state', function() {

scope.bulb = powerSwitchCtrl.getState();

});

}

};

});

By HenryTao – http://henrytao.me - https://mysquar.com 12

CONTROLLER & REQUIRE

Page 13: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

Best PracticeUse controller when you want to

expose an API to

other directives,otherwise use link

13

Page 14: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com 14

Advance

Page 15: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

Meditation

15

Page 16: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

SCOPE

Inheritance scopeng-controller, directive with scope:true, ng-include, ng-switch

isolate scope@ for value (1 way binding), = for 2 ways binding, & for method

transclude scope

ng-repeat type scope

16

Page 17: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

SCOPE - Inheritance

17

<div>

Inside Div One: {{ property }}

<div ng-controller="PropertyCtrl">

Inside Div Two: {{ property }}

</div>

</div

$rootScope

property

PropertyCtrl

property

x

Page 18: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

SCOPE – ngRepeat

18

<div>{{property}}</div>

<div ng-repeat="i in [1, 2, 3, 4, 5]">

<span>{{i}} - {{property}}</span>

</div>

$rootScope

property

ngRepeat

Interation 1

x

new: i

property

Interation 2 …

Page 19: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

SCOPE – Isolate

@ for value (1 way binding)

= for 2 ways binding

& for method

19

Page 20: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

SCOPE – Transclude

Same like isolate @

for value (1 way binding)

20

Page 21: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

TRANSCLUDE

Directive

with replace = falseDirective

with replace = true

Transclude

true

Transclude

element

21

Page 22: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

TRANSCLUDE

22

<div my-directive></div>

<span>hello moto</span>

<div my-directive></div>

<div my-directive>

<span>hello moto</span>

</div>

<div my-directive>

<span>hello moto</span>

</div>

<div ng-transclude><div>

<div my-directive>

<div ng-transclude>

<span>hello moto</span>

</div>

</div>

<div my-directive>

<div><span>hello

moto</span></div>

</div>

<div>

<span>hello moto</span>

</div>

Page 23: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

COMPILE VS LINK

23

return {

restrict: 'E',

compile: function(tElement, tAttrs){

console.log(name + ': compile');

return {

pre: function($scope, $element, $attrs){

console.log(name + ': pre link');

},

post: function($scope, $element, $attrs){

console.log(name + ': post link');

$scope.name = name;

}

}

}

}

<level-one>

<level-two>

<level-three>

{{name}}

</level-three>

</level-two>

</level-one>

What’s the order of console output?

Page 24: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

COMPILE VS LINK

24

Page 25: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

COMPILE VS LINK

25

Page 26: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

Hacking times

26

Page 27: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

Hacking times

27

1. Priority: create dependency directive

http://plnkr.co/edit/R47PiQQWPHS1gO5tt2DL?p=preview

2. Terminal: experiment

http://plnkr.co/edit/b8PnNsNvsFpvX2BGFsUC?p=preview

3. Controller & require: create switch and light

bulb directive

http://plnkr.co/edit/XqQTpsvcvTpczFe8Othi?p=preview

Page 28: Angular Deep Directive

By HenryTao – http://henrytao.me - https://mysquar.com

Hacking times

28

4. Scope: experiment

http://plnkr.co/edit/dwknaG9t1nyFTISWCsuU?p=preview

5. Transclude: experiment transclude element

http://plnkr.co/edit/6yaz13IUodsbddku4ahA?p=preview

6. Nested directive: create 3 level directive

and print out the result order.

http://plnkr.co/edit/qXytDlRhD9wPrw3uKIVz?p=preview

Page 29: Angular Deep Directive

Resources & References

The Hitchhiker’s Guide to the Directivehttps://amitgharat.wordpress.com/2013/06/08/the-hitchhikers-guide-to-the-directive/

Compile VS Link functions inside Directivehttp://www.jvandemo.com/the-nitty-gritty-of-compile-and-link-functions-inside-angularjs-directives/

AngularJS official $compile dochttps://docs.angularjs.org/api/ng/service/$compile

AngularJS Biblehttps://www.ng-book.com/

Understanding Scopehttps://github.com/angular/angular.js/wiki/Understanding-Scopes

Plnkr.co Sample CodePriority http://plnkr.co/edit/R47PiQQWPHS1gO5tt2DL?p=previewTerminal http://plnkr.co/edit/b8PnNsNvsFpvX2BGFsUC?p=previewDirective to Directive http://plnkr.co/edit/XqQTpsvcvTpczFe8Othi?p=previewScope http://plnkr.co/edit/dwknaG9t1nyFTISWCsuU?p=previewTransclude http://plnkr.co/edit/6yaz13IUodsbddku4ahA?p=previewCompile & Link http://plnkr.co/edit/qXytDlRhD9wPrw3uKIVz?p=preview

By HenryTao – http://henrytao.me - https://mysquar.com Images source: https://google.com & above resources 29

Page 30: Angular Deep Directive

Resources & References

By HenryTao – http://henrytao.me - https://mysquar.com 30

Page 32: Angular Deep Directive

Q&A

By HenryTao – http://henrytao.me - https://mysquar.com 32

[email protected]

github.com/henrytao-me

angularify.org

https://github.com/henrytao-me/workshop-angular-directive