angularjs - introductions to directives

13
Egor Miasnikov 1

Upload: egor-miasnikov

Post on 10-May-2015

290 views

Category:

Software


2 download

DESCRIPTION

Introductions to Directives - common things

TRANSCRIPT

Page 1: AngularJS - Introductions to Directives

Egor Miasnikov 1

Page 2: AngularJS - Introductions to Directives

Intro

Custom directive structure

Restrict

Template

Replace

Scope

Transclude

Link

Compile

Bootstrap UI examples

2

Page 3: AngularJS - Introductions to Directives

<body>

<my-awesome-site></my-awesome-site>

</body>

3

Page 4: AngularJS - Introductions to Directives

app.directive(‘myDirective', function() {

return {

restrict: 'AEC',

replace: true,

template: '<p style="color: {{color}}">Hello World</p>',

link: function(scope, elem, attrs) {

},

compile: function(tElem,attrs) {

}

};

});

4

Page 5: AngularJS - Introductions to Directives

A – attribute,

Example: <div my-directive></div>

E – element,

Example: <my-directive></ my-directive>

C - class

Example: <span class=“my-directive”></ span>

5

Page 6: AngularJS - Introductions to Directives

Template – inline string

Example: '<p style="color: {{color}}">Hello World</p>‘

TemplateUrl – url to template.html

Example: ‘/views/template.html’

6

Page 7: AngularJS - Introductions to Directives

Replace: trueExample: <my-directive></ my-directive>

=> <div>Hello world</div>

Replace: falseExample: <my-directive></ my-directive>

=> <my-directive>

<div>Hello world</div>

</my-directive>

7

Page 8: AngularJS - Introductions to Directives

Scope: true

Use a child scope that inherits from parent

Scope: false //default

Don’t have any scopes, only parent

Scope: {}

Use a new isolated scope

8

Page 9: AngularJS - Introductions to Directives

Scope: {

variable1: ‘@’,

variable2: ‘=‘,

func: ‘&’

}

Use “@” for One Way Text Binding

Use “=“ for Two Way Binding

Use “&” to Execute Functions in the Parent Scope

Example: Link

9

Page 10: AngularJS - Introductions to Directives

Transclude: true

Example: <my-directive>Hello</my-directive>

Template: <div ng-transclude></div>

On page: <div>Hello</div>

10

Page 11: AngularJS - Introductions to Directives

link: function(scope, elem, attrs) {

scope.color = "white";

}

11

Page 12: AngularJS - Introductions to Directives

compile: function(tElem,attrs) {

//do optional DOM transformation here

return function(scope,elem,attrs) {

//linking function here

};

}

12

Page 13: AngularJS - Introductions to Directives

?

13