2016-02-16 70 views
0

我在ng類模板中的控制器中引用變量的值,但它不起作用。變量值沒有在控制器中使用ng指令使用指令

下面是HTML指令模板網址:

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

這裏是我使用的指令在索引頁

<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> 

這裏是指令: 在這個指令我是創建一個變量並將其設置爲false,並嘗試將其用於ng級屬性中

(function(window){ '嚴格使用';

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; 

      } 
     } 

    } 
    } 

})();

+0

以什麼方式不工作?發生了什麼事情,這與你所期望的有什麼不同? –

+0

@HarrisWeinstein它沒有在ng-calss中傳遞它,但它在表達式{{tooltipContentValue}}中運行良好 它不會顯示我爲false ..它只是直接顯示tooltipContentValue – Softwarex3

+0

沒有足夠的信息,例如我們不能看看哪裏'keyupevt'被使用...創建一個演示覆制問題 – charlietfl

回答

0

試試這個工作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>