考慮使用angular.jsAngular.js NG-點擊標籤事件被解僱兩次
Plunkr這裏下面的代碼:http://plnkr.co/edit/i4MAzs
HTML:
<form name="myForm" ng-controller="Ctrl">
Try clicking on the labels. <br/>
<label>
Value1: <input type="checkbox" ng-checked='value1' ng-click='toggleValue1()'>
</label> <br/>
<label ng-click='toggleValue2()'>
Value2: <input type="checkbox" ng-checked="value2">
</label> <br/>
<tt>value1 = {{value1}}</tt><br/>
<tt>value2 = {{value2}}</tt><br/>
<tt>fire_count = {{fire_count}}</tt>
</form>
的Javascript:
angular.module('App', []);
function Ctrl($scope) {
$scope.value1 = true;
$scope.value2 = 'YES'
$scope.fire_count = 0;
$scope.toggleValue1 = function(){
$scope.value1 = !$scope.value1;
$scope.fire_count++;
console.log("Clicked!");
}
$scope.toggleValue2 = function(){
$scope.value2 = !$scope.value2;
$scope.fire_count++;
console.log("Clicked!");
}
}
點擊「Value2」標籤時,點擊事件將會觸發兩次。只有在ng-click連接到標籤時纔會發生這種情況。當它連接到輸入元素時,所有事情都按預期工作。
有人可以解釋發生了什麼?
這很奇怪,如果您將「label」更改爲「div」,它可以正常工作 –