Last active
August 29, 2015 14:04
-
-
Save andrew-t/660710b8b02584db1727 to your computer and use it in GitHub Desktop.
Angular (in this case) bubbling problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.js"></script> | |
<script src="script.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="app"> | |
<div ng-controller="con"> | |
<ul ng-click="outerClick($event)" | |
style="margin: 0; padding: 0"> | |
<li style="display: inline-block; | |
width: 50px; height: 50px; | |
background: white; | |
margin: 10px; | |
box-shadow: 0 0 10px black; | |
font-size: 45px; font-family: sans-serif; | |
text-align: center; | |
text-decoration: none; | |
padding: 10px; | |
border-radius: 15px; | |
position: relative" | |
href="#" | |
ng-repeat="link in links" | |
id="link-{{link.a}}"> | |
{{link.a}} | |
<a href="#" | |
style="position: absolute; | |
font-size:20px; | |
text-decoration: none; | |
top: 0px; | |
right: 5px" | |
ng-click="innerClick($event)">×</a> | |
</li> | |
</ul> | |
<p>{{console}}</p> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('app', []); | |
function con($scope) { | |
function reset() { | |
$scope.links = []; | |
for (var i = 0; i < 20; ++i) | |
$scope.links.push({ | |
a: i + 1 | |
}); | |
} | |
$scope.innerClick = function(e) { | |
var el = angular.element(e.target).scope().link; | |
var i = $scope.links.indexOf(el); | |
if (i >= 0) { | |
$scope.links.splice(i, 1); | |
// $scope.links.push(el); | |
} | |
}; | |
$scope.outerClick = function(e) { | |
$scope.console = ''; | |
var t = e.target; | |
while (t) { | |
$scope.console = t.tagName + | |
(t.id ? ' (' + t.id + ')' : '') + | |
' / ' + $scope.console; | |
t = t.parentElement; | |
} | |
e.preventDefault(); | |
}; | |
reset(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment