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:
- ng-app − This directive starts an AngularJS Application.(without this,all angularjs code is nothing.).
- ng-init − This directive initializes application data.
- ng-model − This directive defines the model that is variable to be used in AngularJS.(for data binding).
- ng-repeat − This directive repeats html elements for each item in a collection.(Loop purpose).
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
- <span class="info_icon" style="white-space: nowrap; vertical-align: middle; float: left;"><%-- showhovertable is Custom Directive for tooltip hide/show --%>
- <div class="appTooltipDiv" style="display: none;" showhovertable>
- <table id="tbltooltip" class="tooltip" style="position: absolute; margin: 0px 0px 0px 12px;
- z-index: 1; border-collapse: initial; width: auto;">
- <tr>
- <td class="apptooltipheader">Date:</td>
- <td class="apptooltipheaderData">
- <b>{{x.APP_DATE}}</b>
- </td>
- <td class="apptooltipheader">Time:</td>
- <td class="apptooltipheaderData">
- <b>{{x.TIME_FROM}}</b>
- </td>
- </tr>
- <tr>
- <td class="apptooltipheader">Status:</td>
- <td class="apptooltipheaderData">
- <b>{{x.APPOINTMENT_STATUS_DESCRIPTION}}</b>
- </td>
- <td class="apptooltipheader">Notes:</td>
- <td style="white-space: pre; padding: 5px 10px 5px 5px;">
- <b>{{x.NOTES|limitTo:70}}
- </b>
- </td>
- </tr>
- <tr>
- <td class="apptooltipheader">Reason:</td>
- <td style="white-space: pre; padding: 5px 10px 5px 5px;">
- <b>
- {{x.REASON_DESCRIPTION}}</b>
- </td>
- <td class="apptooltipheader">Provider:</td>
- <td style="padding: 5px 10px 5px 5px;">
- <b>{{x.PROVID_LNAME}},{{x.PROVID_FNAME}}
- </b>
- </td>
- </tr>
- <tr>
- <td style="white-space: nowrap; padding: 5px 5px 5px 10px;">Date Created:</td>
- <td class="apptooltipheaderData">
- <b>{{x.Created_Date | AppDateFilter |
- date:'MM/dd/yyyy'}}</b>
- </td>
- <td class="apptooltipheader">Location:</td>
- <td style="padding: 5px 10px 5px 5px;">
- <b>{{x.LOCATION_NAME}}</b>
- </td>
- </tr>
- <tr>
- <td class="apptooltipheader">Created By:</td>
- <td class="apptooltipheaderData">
- <b>{{x.Created_By}}</b>
- </td>
- </tr>
- <tr>
- <td style="white-space: nowrap; padding: 5px 5px 5px 10px;">Date Modified:</td>
- <td class="apptooltipheaderData">
- <b>{{ x.Modified_Date | AppDateFilter |
- date:"MM/dd/yyyy"}}</b>
- </td>
- </tr>
- <tr>
- <td class="apptooltipheader">Modified By:</td>
- <td class="apptooltipheaderData">
- <b>{{x.Modified_By}}</b>
- </td>
- </tr>
- </table>
- </div>
- </span>
- function ()
- {
- var app = angular.module("myApp", []);
- // Directive for ToolTip
- app.directive('showhovertable', function ()
- {
- return {
- link: function (scope, element, attrs)
- {
- element.parent().bind('mouseenter', function ()
- {
- element.show();
- });
- element.parent().bind('mouseleave', function ()
- {
- element.hide();
- });
- }
- }
- });
- });

Former memberPosted Feb 5, 2016, 11:49 PM
It will be very good if you write example running code into plunker and put your link here. So, reader can easily find the code example.
Santhakumar MunuswamyPosted Jan 27, 2016, 2:11 PM
Have you posted article as Custom Directive or Custom Filter ?
Muhammad Aqib ShehzadPosted Jan 22, 2016, 1:26 PM
good one dear.