javascript - Angular directive with click event gets called twice - Stack Overflow

I'm having an issue where my click event is getting called twice. Not sure what I'm missing b

I'm having an issue where my click event is getting called twice. Not sure what I'm missing but I've only included this directive once in my application. I have tried adding the directive to one element only but the call is still happening twice.

Can someone take a look at the code and tell me what i'm missing please?

angular.module('mymodule', [])
.directive('mydirective', function () {
    return {
        restrict: 'AE',
        link: function ($scope, $element, $attrs) {

            $element.children().bind('click', function () {

                console.log("test" + this);

                if ($element.hasClass('active')) {
                    $element.removeClass('active');
                    return;
                } else if (!$element.hasClass('active')) {
                    $element.addClass('active');
                    return;
                }
            });
        }
    }
});

I'm having an issue where my click event is getting called twice. Not sure what I'm missing but I've only included this directive once in my application. I have tried adding the directive to one element only but the call is still happening twice.

Can someone take a look at the code and tell me what i'm missing please?

angular.module('mymodule', [])
.directive('mydirective', function () {
    return {
        restrict: 'AE',
        link: function ($scope, $element, $attrs) {

            $element.children().bind('click', function () {

                console.log("test" + this);

                if ($element.hasClass('active')) {
                    $element.removeClass('active');
                    return;
                } else if (!$element.hasClass('active')) {
                    $element.addClass('active');
                    return;
                }
            });
        }
    }
});
Share Improve this question asked Jan 27, 2014 at 9:57 user686483user686483 1,5066 gold badges19 silver badges29 bronze badges 2
  • 1 $element.toggleClass('active'); return; for all your condition there. Not sure why double click is happening... try event.stopPropagation(); – Ashish Kumar Commented Jan 27, 2014 at 10:01
  • try adding the click event to parent element and not to children, for example try $element.parent()..bind('click', function () ... – Alex Choroshin Commented Jan 27, 2014 at 10:02
Add a ment  | 

2 Answers 2

Reset to default 5

use this inside the click function to stop bubbling the event

 e.stopPropagation();

To know more check this

I know I'm late in answering this, and a few months back I was facing this issue too. However, a gentleman over here asked me to append this into the directive. And, it worked great!

e.stopImmediatePropagation();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745400563a4626080.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信