We will look and discuss how Directives Work in AngularJS in very easy way.

So first of all What is the Directive in AngularJS, then we will try to create sample so that we can use directive in our application.

"In Simple words, AngularJS directives are used to extend HTML.".

What does that mean,now here we discuss.

There are many built in directives in Angularjs ,but we can create custom directives for our Use. Like:

Above are all built in Directives, Now we will create CustomDirective.

I am working on project where we need to show "Tooltip",so we will implement this.

Step 1:

In front end,Add

  1. <span class="info_icon" style="white-space: nowrap; vertical-align: middle; float: left;"><%-- showhovertable is Custom Directive for tooltip hide/show --%>
  2. <div class="appTooltipDiv" style="display: none;" showhovertable>
  3. <table id="tbltooltip" class="tooltip" style="position: absolute; margin: 0px 0px 0px 12px;
  4. z-index: 1; border-collapse: initial; width: auto;">
  5. <tr>
  6. <td class="apptooltipheader">Date:</td>
  7. <td class="apptooltipheaderData">
  8. <b>{{x.APP_DATE}}</b>
  9. </td>
  10. <td class="apptooltipheader">Time:</td>
  11. <td class="apptooltipheaderData">
  12. <b>{{x.TIME_FROM}}</b>
  13. </td>
  14. </tr>
  15. <tr>
  16. <td class="apptooltipheader">Status:</td>
  17. <td class="apptooltipheaderData">
  18. <b>{{x.APPOINTMENT_STATUS_DESCRIPTION}}</b>
  19. </td>
  20. <td class="apptooltipheader">Notes:</td>
  21. <td style="white-space: pre; padding: 5px 10px 5px 5px;">
  22. <b>{{x.NOTES|limitTo:70}}
  23. </b>
  24. </td>
  25. </tr>
  26. <tr>
  27. <td class="apptooltipheader">Reason:</td>
  28. <td style="white-space: pre; padding: 5px 10px 5px 5px;">
  29. <b>
  30. {{x.REASON_DESCRIPTION}}</b>
  31. </td>
  32. <td class="apptooltipheader">Provider:</td>
  33. <td style="padding: 5px 10px 5px 5px;">
  34. <b>{{x.PROVID_LNAME}},{{x.PROVID_FNAME}}
  35. </b>
  36. </td>
  37. </tr>
  38. <tr>
  39. <td style="white-space: nowrap; padding: 5px 5px 5px 10px;">Date Created:</td>
  40. <td class="apptooltipheaderData">
  41. <b>{{x.Created_Date | AppDateFilter |
  42. date:'MM/dd/yyyy'}}</b>
  43. </td>
  44. <td class="apptooltipheader">Location:</td>
  45. <td style="padding: 5px 10px 5px 5px;">
  46. <b>{{x.LOCATION_NAME}}</b>
  47. </td>
  48. </tr>
  49. <tr>
  50. <td class="apptooltipheader">Created By:</td>
  51. <td class="apptooltipheaderData">
  52. <b>{{x.Created_By}}</b>
  53. </td>
  54. </tr>
  55. <tr>
  56. <td style="white-space: nowrap; padding: 5px 5px 5px 10px;">Date Modified:</td>
  57. <td class="apptooltipheaderData">
  58. <b>{{ x.Modified_Date | AppDateFilter |
  59. date:"MM/dd/yyyy"}}</b>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td class="apptooltipheader">Modified By:</td>
  64. <td class="apptooltipheaderData">
  65. <b>{{x.Modified_By}}</b>
  66. </td>
  67. </tr>
  68. </table>
  69. </div>
  70. </span>
Showhovertable will be Our Directive Name ,Now in Our App.js:
  1. function ()
  2. {
  3. var app = angular.module("myApp", []);
  4. // Directive for ToolTip
  5. app.directive('showhovertable', function ()
  6. {
  7. return {
  8. link: function (scope, element, attrs)
  9. {
  10. element.parent().bind('mouseenter', function ()
  11. {
  12. element.show();
  13. });
  14. element.parent().bind('mouseleave', function ()
  15. {
  16. element.hide();
  17. });
  18. }
  19. }
  20. });
  21. });
Now save it and refresh the page, on hover <span>, we get Tooltip.