【问题标题】:Angular Click outside of an element in directive指令中元素外部的角度单击
【发布时间】:2017-08-25 14:21:15
【问题描述】:

css

ul {
   display: none
}
span.active+ul {
   display:block
}

html

<div ng-controller='exchangeFormCtr'>
    <div toggle-class="active">
       <ul>
           <li ng-repeat='carrency in carrencies'>
       </ul>
    </div>
    <div toggle-class="active">
       <ul>
           <li ng-repeat='carrency in carrencies'>
       </ul>
    </div>   
</div>

控制器

exchange.controller('exchangeFormCtr',['$scope', function($scope) {
    $scope.carrencies = [
        {name:'mastercard'},
        {name:'visa'},
        {name:'paypal'}
    ];
}]);

指令

app.directive('toggleClass', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function() {
                console.log(element);
                element.toggleClass(attrs.toggleClass);
            });
    }
};

当我单击 span 指令更改类以激活或删除它时。

如何捕捉外部点击事件并通过角度方式删除活动类?

【问题讨论】:

  • 为什么不只使用ng-class?它似乎可以满足您的需求
  • 你也可以将'blur'事件绑定到你的元素

标签: javascript angularjs


【解决方案1】:

您可以沿元素单击的 stopPropagation() 将单击处理程序添加到文档对象。

var app = angular.module('my-app', [], function () {})

app.controller('AppController', function ($scope) {
    $scope.message = "Welcome";
});

app.directive('toggleClass', function ($document) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            console.log(element)

            function elementClick(e) {
                e.stopPropagation();
                element.toggleClass(attrs.toggleClass);
            }

            function documentClick(e) {
                element.removeClass(attrs.toggleClass);
            }

            element.on('click', elementClick);
            $document.on('click', documentClick);

            // remove event handlers when directive is destroyed
            scope.$on('$destroy', function () {
                element.off('click', elementClick);
                $document.off('click', documentClick);
            });
        }
    };
});
.active {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app" ng-controller="AppController">
  <span toggle-class="active">ff</span> 
</div>
asdf

【讨论】:

  • 我可以停用其他跨度吗?如果单击一个,另一个具有相同指令的将被删除 .active class ?
【解决方案2】:

您的指令中缺少一些结束字符。 见this working fiddle

myApp.controller('exchangeFormCtr',['$scope', function($scope) {
    $scope.carrencies = [
        {name:'mastercard'},
        {name:'visa'},
        {name:'paypal'}
    ];
}

myApp.directive('toggleClass', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('click', function() {
                console.log(element);
                element.toggleClass(attrs.toggleClass);
            });
        }
    }
});

【讨论】:

    【解决方案3】:

    使用ng-class 和一些filters 提取选定的值

    var exchange = angular.module('App', []);
    
    exchange.controller('exchangeFormCtr', ['$scope',
      function($scope) {
        $scope.carrencies = [{
          name: 'mastercard'
        }, {
          name: 'visa'
        }, {
          name: 'paypal'
        }];
    
        $scope.selectAll = function() {
          angular.forEach(
            $scope.carrencies, function(carrency) {
              console.log('ici')
              carrency.active = true;
            });
        }
      }
    ]);
    .active {
      background-color: green;
      color: orange;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="App" ng-controller="exchangeFormCtr">
      <span ng-click="selectAll()">select all!</span>
      <p ng-repeat="selected in carrencies">
        <span ng-class="{active: selected.active}" 
              ng-click="selected.active = !selected.active">
          {{selected.name}}
          {{selected.active}}
        </span>
      </p>
    
      <p>actives are : <span>{{ carrencies | filter: {active:true} }}</span></p>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多