简体   繁体   English

ng-click不触发按钮单击

[英]ng-click not firing on a button click

I am facing weird error where I am not able to call a function on ng-click when button is clicked. 我遇到奇怪的错误,当单击按钮时,我无法在ng-click上调用函数。

This is the block of code for which the ng-click is not working for delete button but working for edit button. 这是ng-click不适用于删除按钮但适用于编辑按钮的代码块。

  <table ng-table="tableParams" class="table table-striped table-bordered Pages_table">
                        <thead>
                            <td><b>Id</b></td>
                            <td><b>Image</b></td>
                            <td><b>Alt</b></td>
                            <td><b>Edit</b></td>
                            <td><b>Delete</b></td>
                        </thead>
                        <tbody>
                            <tr ng-repeat="dat in newData">
                                <td>((dat.id))</td>    
                                <td class="clients_review_admin_item"><img src="((imagePath+'/'+dat.image))" alt="" style="height:100px;width:150px" /></td>
                                <td>((dat.alt))</td>                                                    
                                <td><button type="button" class="btn btn-info rounded-buttons" data-toggle="modal" data-target="#editWhy" ng-click="editWhy(dat)" ><i class="fa fa-pencil"></i></button></td>
                                <td>
                            <button class="btn btn-danger rounded-buttons" type="button"   ng-click="deleteFooter(dat.id,dat.alt)"><i class="fa fa-close"></i></button>
                                </td>
                            </tr>
                        </tbody>
                    </table>

The Javascript function deleteFooter Javascript函数deleteFooter

$scope.deleteFooter = function(id,name){
console.log("called");
     var postUrl = apiUrl+"/delfriendfooter";
      var $inputs = {
         id:id,
         token:$scope.token,
      };
     var func = function(){
                 $http.post(postUrl, $inputs).
                   success(function(data, status, headers, config) {        
                         swal("Deleted!", name+" has been deleted!", "success");     
                         var dat = $scope.newData;
                         for(var i=0,len= dat.length;i<len;i++){
                             if(dat[i].id==id){
                                 dat.splice(i,1);
                                 break;
                             }
                         }                                                                  
                   }).
                   error(function(data, status, headers, config) {        
                     sweetAlert("Oops!!", "Please try again!!", "error");
                   });
     }

 $scope.deletePopUp(func,name);                       

}; };

The weird thing is that it is working for the edit functionality 奇怪的是,它正在使用编辑功能

 <td><button class="btn btn-info rounded-buttons" data-toggle="modal" data-target="#editWhy" ng-click="editWhy(dat)" ><i class="fa fa-pencil"></i></button></td>

I have also tried to call other functions but it is just not firing. 我也曾尝试调用其他函数,但它没有触发。

Note: I have changed the angular braces to (()). 注意:我已将角括号更改为(())。

This is happening because of the way angular / buttons / forms work . 这是由于角度/按钮/表格的工作方式而发生的。

Specifically: 特别:

To prevent double execution of the handler, use only one of the ngSubmit or ngClick directives. 为防止重复执行该处理程序,请仅使用ngSubmit或ngClick指令之一。 This is because of the following form submission rules in the HTML specification: 这是由于HTML规范中的以下表单提交规则:

  • If a form has only one input field then hitting enter in this field triggers form submit (ngSubmit) 如果表单只有一个输入字段,则在此字段中按回车会触发表单提交(ngSubmit)
  • If a form has 2+ input fields and no buttons or input[type=submit] then hitting enter doesn't trigger submit 如果表单具有2个以上的输入字段,并且没有按钮或input[type=submit]则按Enter不会触发提交
  • If a form has one or more input fields and one or more buttons or input[type=submit] then hitting enter in any of the input fields will trigger the click handler on the first button or input[type=submit] ( ngClick ) and a submit handler on the enclosing form ( ngSubmit ) 如果表单具有一个或多个输入字段以及一个或多个按钮或input[type=submit]则在任何输入字段中按回车都会触发第一个按钮或input[type=submit]ngClick )上的单击处理程序。 ngSubmitngSubmit )上的提交处理程序

Because of these rules, buttons can behave a little strangely. 由于这些规则,按钮的行为可能会有些奇怪。

A solution is to add type="button" to your buttons. 一种解决方案是将type="button"添加到您的按钮中。 This will basically prevent these rules from being applied: 这将基本上阻止应用以下规则:

<button type="button" class="btn btn-danger rounded-buttons"  ng-click="deleteFooter(dat.id,dat.alt)"><i class="fa fa-close"></i></button>

Angular will then, instead of using the form standards to decide what function to call, simply execute the ng-click 's function. 然后,Angular将代替执行表单标准来确定要调用的函数,而只需执行ng-click的函数即可。

The problem is that i guess deleteFooter function and it's ng-click is called on different scopes. 问题是我猜测deleteFooter函数和它的ng-click在不同的作用域上被调用。 Both are defined in your controllers scope, and its fine. 两者都在您的控制器范围内定义,并且很好。 But i suppose, as You wrote, that you call deleteFooter in ng-repeat, which has its own scope. 但是我想,正如您所写的那样,您在ng-repeat中调用deleteFooter,它具有自己的作用域。

Use $parent to enforce your controller scope on deleteFooter. 使用$ parent在deleteFooter上强制执行您的控制器作用域。

<button class="btn btn-danger rounded-buttons"  ng-click="$parent.deleteFooter(dat.id,dat.alt)"><i class="fa fa-close"></i></button>

Ok so after so many efforts I opted for jquery ajax method and applied the changes using angular $apply to the scope variable. 好了,经过这么多的努力,我选择了jquery ajax方法,并使用angular $ apply将更改应用于范围变量。 I am still not sure what is wrong. 我仍然不确定什么地方出了问题。

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

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