我有嵌套的ul \ li元素。我对所有li元素都有相同的ng-click。出于某种原因,如果我单击嵌套在另一个li元素下的li元素,则只触发父元素。在下面的例子中,当我点击“Sub One”li元素时,它会两次触发'Two'li元素。我知道如何停止传播,但它仍会触发父ng-click。希望这是有道理的。
<ul>
<li ng-click="doSomething()">One</li>
<li ng-click="doSomething()">Two
<ul>
<li ng-click="doSomething()">Sub One</li>
<li ng-click="doSomething()">Sub Two</li>
</ul>
</li>
<li ng-click="doSomething()">Three</li>
</ul>
答案 0 :(得分:5)
你可能犯了错误,因为它完美无缺。根据{{3}},只需使用$event.stopPropagation()
即可。例如,尝试this question:
<ul ng-app="test" ng-controller="MyCtrl">
<li ng-click="doSomething($event, 1)">One</li>
<li ng-click="doSomething($event, 2)">Two
<ul>
<li ng-click="doSomething($event, 21)">Sub One</li>
<li ng-click="doSomething($event, 22)">Sub Two</li>
</ul>
</li>
<li ng-click="doSomething($event, 3)">Three</li>
</ul>
和:
function MyCtrl($scope) {
$scope.doSomething = function ($event, test)
{
console.log(test);
$event.stopPropagation();
}
}
然后点击“Sub One”只需在控制台中显示21。