简体   繁体   English

AngularJS ng-click 触发两次

[英]AngularJS ng-click fires twice

I am using ng-click and it fires twice when I apply it to SPAN tag.我正在使用 ng-click,当我将它应用到 SPAN 标签时它会触发两次。

HTML 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 ) http://jsfiddle.net/kNL6E/ (点击Save

Perhaps you included Angular twice?也许您两次包含 Angular? If you do that, you'll get two alerts, demonstrated here:如果您这样做,您将收到两个警报,如下所示:

http://jsfiddle.net/kNL6E/1/ http://jsfiddle.net/kNL6E/1/

I had a similar problem, but could not find where I included Angular twice in my files.我遇到了类似的问题,但找不到我在文件中两次包含 Angular 的位置。

I was using an ajax calls to load specific form layouts, so I need to use $compile to activate Angular on the inserted DOM.我正在使用 ajax 调用来加载特定的表单布局,所以我需要使用$compile在插入的 DOM 上激活 Angular。 However, I was using $compile on my directive $element which has a controller attached.但是,我在我的指令$element上使用了$compile ,它附加了一个控制器。 Turns out this "includes" the controller twice, causing two triggers with ng-click or ng-submit .事实证明,这两次“包含”了控制器,导致两次触发ng-clickng-submit

I fixed this by using $compile directly on the inserted DOM instead of my directive $element .我通过直接在插入的 DOM 上使用$compile而不是我的指令$element

Same problem using同样的问题使用

<a ng-click="fn()"></a>

fn was called twice fn 被调用了两次

Using a button fixed it :使用按钮修复它:

<button ng-click="fn()"></button>

This is probably obscure, but I had ng-click firing twice because I had BrowserSync running, which would mirror my inputs into a tab that I had open in another window, thus doubling up all my clicks.这可能是模糊的,但我有两次ng-click触发,因为我运行了 BrowserSync,这会将我的输入镜像到我在另一个窗口中打开的选项卡中,从而使我的所有点击次数加倍。 To resolve, I disabled “ghostMode”: https://www.browsersync.io/docs/options/为了解决,我禁用了“ghostMode”: https ://www.browsersync.io/docs/options/

I solved this by removing my ngsubmit handler as I don't have a need for it.我通过删除我的 ngsubmit 处理程序解决了这个问题,因为我不需要它。 I'm monitoring on change events and using SignalR to update the screen in near real-time.我正在监视更改事件并使用 SignalR 近乎实时地更新屏幕。

I was also in a form and the AngularJS docs for ngSubmit states:我也在一个表单中, ngSubmitAngularJS 文档指出:

Warning: Be careful not to cause "double-submission" by using both the ngClick and ngSubmit handlers together.警告:注意不要通过同时使用ngClickngSubmit处理程序而导致“双重提交”。 See the form directive documentation for a detailed discussion of when ngSubmit may be triggered.有关何时可以触发ngSubmit的详细讨论,请参阅表单指令文档

In case of somebody having the same issue: This was my problem:如果有人遇到同样的问题:这是我的问题:

<a type="submit" ng-click="login()">submit login<a>

Both, the type="submit" and the ng-click="login()" triggered the login()-method in my controller. type="submit"ng-click="login()"触发了我的控制器中的 login() 方法。

So just use either the type=submit or the ng-click directive所以只需使用 type=submit 或 ng-click 指令

If other answers don't help, make sure that AngularJS profiling is disabled in Batarang (if you have it installed of course).如果其他答案没有帮助,请确保在 Batarang 中禁用了 AngularJS 分析(如果你安装了它)。

This was making ng-click to fire twice for me.这让 ng-click 为我触发两次。

在此处输入图片说明

elements wrapped one another, and this can cause trigger event twice or more.元素相互包裹,这可能会导致触发事件两次或更多次。

So i used a simple CSS trick for this solution :所以我在这个解决方案中使用了一个简单的 CSS 技巧:

.off{
   pointer-events:none;
}

and apply it to an element correspond to click event.并将其应用于与单击事件对应的元素。

I got it when I accidently called $compile for the dynamically added elements several times in the cycle, instead of just once.当我在循环中多次为动态添加的元素调用 $compile 而不是一次时,我得到了它。 Compiling just once removed this effect.编译一次就消除了这种影响。

You might have a ng-click inside a form container using a ng-submit.您可能会使用 ng-submit 在表单容器内进行 ng-click。 In that case, add type="button" to all your using ng-click.在这种情况下,将 type="button" 添加到您使用的所有 ng-click 中。

was facing same issue.面临同样的问题。 Find out they were using https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js version and i switch it to latest 1.4.5 version and it just worked.发现他们正在使用https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js版本,我将其切换到最新的 1.4.5 版本,它就可以工作了。

https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js

 var app = angular.module('regApp', []); app.controller('RegistrationCtrl', ['$scope','$http',function($scope,$http ) { $scope.PostRegistration = function() { alert('click '); // <--- fires twice /// some code here -- }; }]);
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="regApp" ng-controller="RegistrationCtrl"> <h2 >Registration for {{data.EventName}}</h2> <span ng-click="PostRegistration()" class="btn" id="btnSave">Save </span> </div>

我已将<button ng-click='clickHandler()'>更改为<a ng-click='clickHandler()'>并且现在事件仅触发一次

I've had the same issue when dynamically injecting partial views in my SPA(Single Page Applications).The way I solved it was to store the content of my div in a local variable like this:在我的 SPA(单页应用程序)中动态注入部分视图时,我遇到了同样的问题。我解决它的方法是将我的 div 的内容存储在这样的局部变量中:

var html = $("#leftContainer").html();

Then empty out the div and reset its content like this:然后清空 div 并像这样重置其内容:

$("#leftContainer").empty();
$("#leftContainer").append(html);

Now you can add any additional html you have received from an AJAX call or dynamically constructed and lastly remember to re-compile your HTML to make it bound to angular:现在,您可以添加从 AJAX 调用或动态构建的任何其他 html,最后记住重新编译您的 HTML 以使其绑定到 angular:

var entities = $("#entitiesContainer");

var entity = "<div><a href='#' ng-click='Left(" + entityId + ")'>&nbsp;" + entityName + "</a><div>"
entities.append(entity);

var leftContainer = angular.element(document.getElementById("entitiesContainer"));

$compile(leftContainer)($scope);

I'm a bit of a dummy and had this:我有点笨,有这个:

<li data-shape="amethyst" ng-click="toggleGem('amethyst')">
    <label></label>
    <i class="amethyst"></i>Amethyst
    <input type="checkbox" name="gem_type" id="gem-amethyst" value="amethyst" />
</li>

The click event was triggering twice.点击事件触发了两次。 I moved the ng-click to the label element and it's working as expected.我将 ng-click 移动到 label 元素,它按预期工作。

<li data-shape="amethyst">
    <label ng-click="toggleGem('amethyst')"></label>
    <i class="amethyst"></i>Amethyst
    <input type="checkbox" name="gem_type" id="gem-amethyst" value="amethyst" />
</li>

This situation may be caused by lacking the ngTouch in Angular.这种情况可能是由于 Angular 中缺少 ngTouch 造成的。

Event if the ngTouch is loaded, the bug in ngTouch and ngClick before Angular 1.5.0 may occur.加载ngTouch的事件,可能会出现Angular 1.5.0之前的ngTouch和ngClick的bug It results from the ngClick being triggered by pointerup and mouseUp in Chrome for Desktop on device toolbar or mobile device.它是由设备工具栏或移动设备上的 Chrome for Desktop 中的pointerupmouseUp触发的pointerup引起的。

I had a problem like this as well.我也有这样的问题。

<button style="width: 100%;" class="btn btn-danger" ng-click="'{{vm.delete()}}'">

Thats not how you call ng-click, but no errors were thrown and the function calls still worked.那不是您调用 ng-click 的方式,但是没有抛出错误并且函数调用仍然有效。

<button style="width: 100%;" class="btn btn-danger" ng-click="vm.delete()">

Is correct and will then only be called once.是正确的,然后只会被调用一次。

I don't know what was the reason this happened, but I had to use event.stopPropagation();我不知道发生这种情况的原因是什么,但我不得不使用event.stopPropagation(); inner my JavaScript function.内部我的 JavaScript 函数。

HTML HTML

<button class="button" ng-click="save($event)">Save</button>

JAVASCRIPT爪哇脚本

function save(event) {
    event.stopPropagation();
}

I have handled it by following code我已通过以下代码处理它

HTML : HTML :

<div>
    <ui>
        <li>
            <button class="Button" ng-disabled="self.desableSubmitButton" ng- 
            click="self.SubmitClick($event)">Submit</button>
        </li>
    </ui> 
</div>

Angular 1.0 Controller :角度 1.0 控制器:

_self.SubmitClick = function(event){   
     _self.desableSubmitButton = true; //disable submit button
     event.preventDefault();
     event.stopPropagation();

     setTimeout(funtion() {
       _self.desableSubmitButton = false; //enable submit button after timeout
           if(_self.$scope.$$phase != '$apply' || _self.$scope.$$phase != '$digest'){
             _self.$scope.$digest();
             }
      }, 1000);//other Code logic
}

My case: I used ng-click event on my custom custom angular checkbox component.我的情况:我在我的自定义自定义角度复选框组件上使用了 ng-click 事件。 So just providing a method binding for custom component did the trick.所以只为自定义组件提供一个方法绑定就可以了。

Before: <mono-checkbox-ng ng-click="vm.onSelectAllClicked()">之前: <mono-checkbox-ng ng-click="vm.onSelectAllClicked()">

After: <mono-checkbox-ng on-changed="vm.onSelectAllClicked()">之后: <mono-checkbox-ng on-changed="vm.onSelectAllClicked()">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM