Angular ng-click $event passes child element as target

后端 未结 3 2058
故里飘歌
故里飘歌 2021-02-19 10:06

For each td element in a table I have an attached ng-click. Here is the (simplified) html for each table cell:



        
相关标签:
3条回答
  • 2021-02-19 10:24

    In Angular 7.x

    myClickHandler($event) {
        this.selectedElement = <Element>$event.target.closest('.list-item');
    }
    

    html:

    <div class="list-item" (click)="myClickHandler($event)">...</div>
    
    0 讨论(0)
  • 2021-02-19 10:35

    Changing:

    angular.element(event.target)
    

    to:

    angular.element(event.currentTarget)
    

    fixed my issue.

    It seems to me using event.currentTarget is preferred to event.target in the majority of usage cases.

    0 讨论(0)
  • 2021-02-19 10:41

    event.target.closest('td') won't work because event.target is a DOM element and it doesn't have method closest. You need to create a jQuery object to use this method.

    Try to find closest td like this:

    angular.element(event.target).closest('td')
    
    0 讨论(0)
提交回复
热议问题