$scope's method called via ng-click: executed twice by IE

后端 未结 2 1040
借酒劲吻你
借酒劲吻你 2020-12-18 01:43

In my controller:

$scope.homeAction = function() {
  console.log(\"HomeAction\");
};

In my view:

相关标签:
2条回答
  • 2020-12-18 02:18

    It seems to be related to the <button> event handling on Internet Explorer. Clicking it dispatches 2 events : MouseEvent and PointerEvent which explains why homeAction is called twice.

    The easiest solution would be to change the <button> element to another DOM element (i.e. <a> or <span>)

    Updated version using an <a> element http://plnkr.co/edit/Nn8CF7TnDKqsJA3unsp6

    Another solution would be to verify which type of Event is dispatched and only allow MouseEvents. You can do this by passing the $event on your HomeAction and check the existence of the pointerType property (which is only available on TouchEvents). An example on plnkr : http://plnkr.co/edit/RmVHT1Pf2IeCNdmDH51T

    $scope.homeAction = function($event) {
        if ($event.originalEvent.pointerType) {
          //PointerEvent, don't do anything
          return;
        }
    
        console.log("HomeAction");
    };
    
    0 讨论(0)
  • 2020-12-18 02:19

    Just add type="button"to your button and it should be fixed. Default behaviour is submit and apparently that messes with your code.

    <ion-view title="Home">
    
      <ion-content padding="true">
        <button type="button" ng-click="homeAction()" class="button button-block button-positive">call homeAction()</button>
      </ion-content>
    
    </ion-view>
    
    0 讨论(0)
提交回复
热议问题