AngularJS ng-click 触发两次 [英] AngularJS ng-click fires twice
本文介绍了AngularJS ng-click 触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ng-click,当我将它应用到 SPAN 标签时它会触发两次.
HTML
<h2>注册{{data.EventName}}</h2><span class="btn" id="btnSave" ng-click="PostRegistration()">保存</span>
控制器
var app = angular.module('regApp', ['ui']);app.controller('RegistrationCtrl', function ($scope, $http) {$scope.PostRegistration = 函数 () {警报('点击');<--- 触发两次///这里有一些代码——};
它应该只触发一次.我如何才能找到发生这种情况的原因以及如何解决?
解决方案
您提供的代码不会触发该事件两次:
http://jsfiddle.net/kNL6E/(点击保存
)
也许你两次包含 Angular?如果您这样做,您将收到两个警报,如下所示:
I am using ng-click and it fires twice when I apply it to SPAN tag.
HTML
<div ng-app="regApp" ng-controller="RegistrationCtrl" data-ng-init="GetEventDetail()" ng-show="data.EventName">
<h2>Registration for {{data.EventName}}</h2>
<span class="btn" id="btnSave" ng-click="PostRegistration()">Save </span>
</div>
CONTROLLER
var app = angular.module('regApp', ['ui']);
app.controller('RegistrationCtrl', function ($scope, $http) {
$scope.PostRegistration = function () {
alert('click '); <--- fires twice
/// some code here --
};
It should only fire once. How I can find why this is happening and how to fix it?
解决方案
The code you've provided does not fire the event twice:
http://jsfiddle.net/kNL6E/ (click Save
)
Perhaps you included Angular twice? If you do that, you'll get two alerts, demonstrated here:
这篇关于AngularJS ng-click 触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文