angular directives - three practical examplesfiles.meetup.com/19897405/angular directives... ·...

20
Angular Directives Three Practical Examples

Upload: others

Post on 09-May-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Angular DirectivesThree Practical Examples

Page 2: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Luis De Avila Full-stack JavaScript

[email protected] www.luisdeavila.com www.linkedin.com/in/ldeavila

Page 3: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Overview• What are Angular Directives?

• The Directive Definition Object

• Practical Examples:

• Cancel Button

• Custom Form Input

• Contextual Navigation Bar

Page 4: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

What is a Directive?• A directive is a function, which executes, when the

Angular compiler encounters the directive’s corresponding marker in the DOM

• Markers can be elements, attributes, css classes, and comments

Page 5: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

What is a Directive?<html> … <my-hello-world></my-hello-world> </html>

angular .module('app') .directive('myHelloWorld', function(){ //The stuffs });

Notice that Angular normalizes camel-case to dashes

Page 6: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Repetition Is Key• A directive is a function, which executes, when the

Angular compiler encounters the directive’s corresponding marker in the DOM

• Examples:

• ng-hide, ng-model

• input, a

Page 7: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

The Directive Definition Object

• Tells the Angular compiler how the directive works

• The directive’s function must return the directive definition object

Page 8: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular
Page 9: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Cancel Button• Use Case: Return the user to the previous page

regardless of how they entered the current page.

Dash

Invoice Detail

Invoices List

Page 10: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

DDO Properties• restrict - Restricts the directive to either element

(E), attribute (A), class (C), or comment (M)

• template/templateUrl - HTML markup for the directive or a path to an HTML file

• link - Function responsible for registering DOM listeners as well as updating the DOM.

function(scope, iElement, iAttrs, …)

Page 11: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Custom Form Input• Use Case: Create a component that displays a

summary of data values to a user so they can compare an initial value with an updated value

Initial Value Updated ValueDescription Difference

Page 12: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

DDO Properties• scope - Can be true, an object or a falsy value

• falsy: directive will use parent’s scope

• true: A new child scope that prototypically inherits from its parent will be created

• object: An "isolate" scope is created for the directive. It does not prototypically inherit from its parent scope

Page 13: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Isolate Scope Prefix • @: One-way binding - Changes in parent are

reflected in directive’s scope. Attribute value is string or interpolated

• =: Two-way binding - Changes are bidirectional. Attribute value is a model

• &: Expression binding - Attribute is an Angular expression that is wrapped in a function that can be called

Page 14: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular
Page 15: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Nav Bar• Use Case: A navigation bar that changes available

options based on user role. Only admins can see the billing link

Profile

Billing

Link 2Link 1

Page 16: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Isolate Scope Prefix

• <: One-way binding - Changes in directive’s scope are not reflected in parent’s scope. Attribute value is a model

• ?: Optional - Makes = and < attributes optional

Page 17: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular
Page 18: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

Thanks

[email protected] www.luisdeavila.com www.linkedin.com/in/ldeavila

Page 19: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular
Page 20: Angular Directives - Three Practical Examplesfiles.meetup.com/19897405/Angular Directives... · What is a Directive? • A directive is a function, which executes, when the Angular

References• https://docs.angularjs.org/api/ng/service/

$compile#directive-definition-object

• https://docs.angularjs.org/guide/directive

• https://docs.angularjs.org/guide/compiler

• http://gifsoup.com/view/4453671/waynes-world.html

• http://thetwistedbuzz.com/a-few-facts-about-waynes-world-that-youve-probably-never-heard-17-photos/