Variable value not passing in a controller using directive with ng-class

211 views Asked by At

I am referencing the value of the variable in a controller in an ng-class template but its not working.

here is the html directive template URl :

<div class="tooltip-anchor">
<div class=" tooltip-content ehub-body" ng-class="{ 'tooltip__content--disabled': tooltipContentValue}" ng-transclude>Tooltip content</div>
</div>

Here is where i am using the directive in the index page

 <div style="text-align:center;">
  <a href="" ng-keyup="keyupevt()"><ehub-tooltip>Hello i am here, and i am her to stay</ehub-tooltip>over here</a>
  <a href="" ng-keyup="keyupevt()"><ehub-tooltip>Be nice to people on your way up and they will be nice to you on your way down</ehub-tooltip>click me</a>
 </div>

And here is the directive: in this directive i am creating a variable and setting it to false and also trying to use it in an ng-class attribute

(function (window) { 'use strict';

angular
  .module('ehub.component.tooltip', [])
    .controller('ehubTooltipCtrl', ['$scope', function ($scope) {
        $scope.tooltipContentValue = false;

    }])
  .directive('ehubTooltip', ehubTooltip);

function ehubTooltip() {
    var directive = {
        controller: "ehubTooltipCtrl",
        link: link,
        transclude: true,
        templateUrl: 'ehub-tooltip.html',
        restrict: 'E'

    };
    return directive;

    function link(scope, element, attrs) {

        scope.keyupevt = function () {
            if (event.keyCode === 27) {
                $scope.tooltipContentValue = true;

            }
         }

     }
  }

})();

1

There are 1 answers

0
Stepan Kasyanenko On

Try this working jsfiddle.

angular.module('ExampleApp', ['ngMessages'])
  .controller('ExampleController', function($scope) {

  })
  .directive('ehubTooltip', function() {
    var directive = {
      link: link,
      transclude: true,
      template: '<div class="tooltip-anchor"><div class=" tooltip-content ehub-body" ng-class="{ \'tooltip__content--disabled\': tooltipContentValue}" ng-transclude>Tooltip content</div></div>',
      restrict: 'E'
    };
     function link(scope, element, attrs) {
      scope.tooltipContentValue = false;
      scope.keyupevt = function() {
        if (event.keyCode === 27) {
          scope.tooltipContentValue = true;
        }
      }

    }
    return directive;
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="ExampleApp">
  <div ng-controller="ExampleController">

    <div style="text-align:center;">
      <a href="" ng-keyup="keyupevt()">
        <ehub-tooltip>Hello i am here, and i am her to stay</ehub-tooltip>over here</a>
      <a href="" ng-keyup="keyupevt()">
        <ehub-tooltip>Be nice to people on your way up and they will be nice to you on your way down</ehub-tooltip>click me</a>
    </div>
  </div>
</div>