Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular Directives For The Rest Of Us

Angular Directives For The Rest Of Us

A very basic introduction to directives. Talk at the Angular Meetup Berlin, April 9th 2014

Rin Raeuber

April 09, 2014
Tweet

More Decks by Rin Raeuber

Other Decks in Programming

Transcript

  1. Angular Directives
    for the rest of us

    View Slide

  2. Rin Räuber
    @rinpaku

    !
    developer at bitcrowd

    View Slide

  3. Content
    what is a directive?
    !
    how to roll your own.
    !
    testing.

    View Slide

  4. Angular
    is what HTML would have been,

    had it been designed

    for building web-apps

    View Slide

  5. lets you build a new vocabulary

    for HTML – a DSL for your app

    Angular

    View Slide

  6. Directives

    View Slide

  7. What the fuck
    is a Directive?

    View Slide

  8. teach your browser new tricks

    !
    add behavior to an element

    and/or transform the DOM
    Directives

    View Slide


  9. THIS

    View Slide

  10. !
    !
    !<br/>$(function() {!<br/>$("#datepicker").datepicker();!<br/>});!<br/>
    instead of this

    View Slide

  11. THIS

    View Slide

  12. instead
    of this

    View Slide

  13. include angular.js

    the setup

    View Slide

  14. include angular.js

    create your Angular app
    angular.module(“myAwesomeAngularApp”, []);
    the setup

    View Slide

  15. include angular.js

    create your Angular app
    angular.module(“myAwesomeAngularApp”, []);
    have Angular bootstrap it

    the setup

    View Slide

  16. the setup
    $ yo angular bitch
    shortcut!

    View Slide

  17. Our first
    Directive

    View Slide


  18. View Slide

  19. myApp.directive('kitten',
    function(){
    return {
    // directive definition object
    };
    }
    );
    Registering a directive

    View Slide

  20. restrictions
    myApp.directive('myDirective',
    function(){
    return {
    restrict: ‘ACE’
    };
    }
    );

    View Slide

  21. restrictions
    A is for attributes

    View Slide

  22. restrictions
    C is for CSS classes


    View Slide

  23. restrictions
    E is for elements

    View Slide

  24. restrictions
    E is for elements

    no kittens
    for IE =< 8

    View Slide

  25. myApp.directive('myDirective',
    function(){
    return {
    template: ‘I am a template’
    };
    }
    );
    template

    View Slide

  26. myApp.directive('myDirective',
    function(){
    return {
    templateUrl: ‘me_too.html’
    };
    }
    );
    templateUrl

    View Slide

  27. let’s look at
    an example.

    View Slide

  28. kittens
    come
    in different
    sizes,
    so …

    View Slide

  29. let’s add an attribute
    height=‘50’>

    View Slide

  30. myApp.directive('myDirective', function(){
    return {
    link: function(scope, element, attrs){
    // do something
    }
    };
    });
    the linkin’ function

    View Slide

  31. modifying the DOM
    function(scope, element, attrs){
    // change the height of the element
    element.css(‘height’, ‘42px’);
    }

    View Slide

  32. let’s look at
    an example.
    again.

    View Slide

  33. … but
    what about
    behavior?

    View Slide

  34. adding an event listener
    function(scope, element, attrs){
    element.on(‘mouseover’, function(){
    alert(‘meooow’);
    })
    }

    View Slide

  35. let’s look at
    an example.
    one last time.

    View Slide

  36. I wish
    I had known™

    View Slide

  37. scope.$apply

    View Slide

  38. scope

    View Slide

  39. code.
    again.

    View Slide

  40. when to use
    scope.$apply

    View Slide

  41. changes to the scope
    that Angular doesn’t
    know about

    View Slide

  42. changes to the scope
    that Angular doesn’t
    know about
    browser DOM events

    View Slide

  43. changes to the scope
    that Angular doesn’t
    know about
    browser DOM events
    setTimeout

    View Slide

  44. changes to the scope
    that Angular doesn’t
    know about
    browser DOM events
    setTimeout
    asynchronous requests

    View Slide

  45. changes to the scope
    that Angular doesn’t
    know about
    browser DOM events
    setTimeout
    asynchronous requests

    ng-click
    $timeout
    $http

    View Slide

  46. … but how
    do I test
    this stuff?

    View Slide

  47. you DO test,
    don’t you?

    View Slide

  48. Unit Testing w/
    Karma and Jasmine

    View Slide

  49. install and configure Karma
    $ npm install -g karma
    $ karma init
    run Karma
    karma start
    rejoice \o/
    the setup

    View Slide

  50. describe(‘my thing', function(){
    // some setup stuff
    !
    it(“does something", function() {
    expect(result).toEqual(expectedResult);
    });
    Jasmine specs

    View Slide

  51. example!

    View Slide

  52. Built-in
    Directives

    View Slide

  53. ng-click

    Kitten

    View Slide

  54. ng-hide=‘hidden’>
    Hide me!

    ng-hide

    View Slide

  55. ng-show=‘shown’>
    Hide me!

    ng-show

    View Slide


  56. I think, therefore I am.

    ng-if

    View Slide

  57. Moar
    Directives

    View Slide

  58. even more awesome angular directives

    View Slide

  59. some words
    of advice.

    View Slide

  60. Don’t try to use “self-closing”

    or void tags for your directives.

    View Slide

  61. Know where to use scope.$apply.

    !
    (And why.)

    View Slide

  62. Read the fucking manual.

    View Slide

  63. Don’t reimplement

    existing directives.

    View Slide

  64. Test.

    View Slide

  65. Recap

    View Slide

  66. … that directives are awesome

    !
    … how to build one

    !
    … how to manipulate the DOM

    and add event listeners in its link function

    !
    … how to test a directive
    you hopefully learnt

    View Slide

  67. kthxbai
    @rinpaku
    abstraction.killedthecat.net

    View Slide

  68. Writing Directives (Talk by Miško Hevery)
    Resources
    unrelated, but awesome:
    Building 2048 in Angular

    View Slide