AngularJS Directives Fundamentals - p-patel/software-engineer-knowledge-base GitHub Wiki

https://app.pluralsight.com/player?course=angularjs-directive-fundamentals Course website - http://github.com/joeeames/angularjsdirectivesfundamentals/wiki

Your First Directive

User Info Card Directive

  • 4 ways to specify a directive - attribute, element, class, comment
  • attributes - modify an existing element, element - adding a new object to the page
  • define directive using angular.module('myModule').directive('userInfoCard', function(){ return { } }). The directive name is used in camel-case in the html file and can be prefixed with 'data' to form valid html <div user-info-card></div>
  • template property: template: "User Info Card"
  • restrict property: restrict: "E". Other options are A, C and M. They can be concatenated to support multiple directive types are required.

Events, Scope and Controllers

  • ...

Decorator Directives

  • Typically add functionality to an existing tag or modifies the display of the tag. Most built-in directives we use are decorator directives. Usually defined as an attribute and usually without a template.

Built-in Decorator Directives

  • ng-app, ng-controller, ng-click, ng-model, ng-show, ng-hide, ng-class are all examples of decorator directives that add functionality of modify the display of a tag

The Link Function

  • Building own decorator directve: include bootstrap-css, jquery, angularjs
  • Add spacebar support to videos to pause/play the video
  •   <source= "video_url" type="video/mp4"/>
    </video>```
    
  • define spacebarSupport directive, no scope or controller required. Modify HTML on the page and register a keypress listener on the body tag using directive's link function. On the registered keypress listener's handler listen for a spacebar keypress and play/pause the video accordingly.
  • if a directive only returns a function it defaults to all default settings and the function is set as the link function
⚠️ **GitHub.com Fallback** ⚠️