2014-12-02 38 views
1

我將ng-mouseleave事件附加到我的html標記中。不幸的是它觸發兩次。一旦用戶離開網站時很好,一旦他進入網站,這是不想要的。我有一個關於事件的console.log。mouseenter上的ng-mouseleave觸發器

的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

這是因爲被觸發的子元素的事件,以及,你需要手動停止這個:

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

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

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

+0

工作正常不知道 – 2014-12-02 17:37:00