My target is pretty simple. There will be an show-overlay directive on images. If you enter mouse, it will wrap with a parent span and append overlay. On mouseleave it will remove the parent span and overlay div. But for some reason if I use replaceWith on mouseleave it causes to fire the mouseenter unexpectedly multiple times for the next enters.
The directive as is below
app.directive('showOverlay', function($compile) {
return {
restrict: 'A',
link: function($scope, $element, attrs) {
$element.on('mouseenter', function (e) {
console.warn('mouseenter');
$el = $element.wrap("<span class='img'></div>")
$el = $el.parent().append("<div ng-mouseleave='cancelEditMode($event)' class='overlay'></div>");
$element.parent().addClass("hover");
var inputElem = $element.parent();
$compile(inputElem)($scope);
});
$scope.cancelEditMode = function(e) {
$element.parent().replaceWith($element);
};
}
};
});
From the above code, looks like the replacewith causes $element to have multiple mouseenter event. jsfiddle is here: http://jsfiddle.net/RmDuw/979/