ng-mouseleave在mouseenter上触发

时间:2014-12-02 16:56:15

标签: angularjs mouseevent

我将ng-mouseleave事件附加到我的html标签。不幸的是它会触发两次一旦用户离开网站这是好的,一旦他进入网站,这是不想要的。我在活动上有一个console.log。

the plunkr: http://run.plnkr.co/plunks/rjfyCw/

的index.html

    <html ng-app="myApp" id="myApp" ng-controller="MainController as mainCtrl" ng-mouseleave="mainCtrl.log('xy')">

Controller.js

myAppControllers.controller('MainController', [ function () {
    this.log = function(log) {
      console.log(log);
    };

  }]);

1 个答案:

答案 0 :(得分:1)

因为事件是在子元素上触发的,所以你需要手动停止:

ng-mouseleave="mainCtrl.log($event, 'xy')"

this.log = function($event, log) {
      $event.stopPropagation()
      console.log(log);
    };

http://plnkr.co/edit/sSpqiIqMz3rVYzrPQ0iC?p=preview