0

我在 ng 类模板的控制器中引用变量的值,但它不起作用。

这是 html 指令模板 URl :

<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-class 属性中使用它

(函数(窗口){'使用严格';

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;

            }
         }

     }
  }

})();

4

1 回答 1

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>

于 2016-02-17T04:48:10.807 回答