简体   繁体   English

AngularJS ng-submit函数未执行

[英]AngularJS ng-submit function not being executed

I'm developing a AngularJS project, and i have a template which displays a table of users, & then has a button (which raises a modal form) to Add a new user. 我正在开发一个AngularJS项目,并且我有一个显示用户表的模板,然后有一个按钮(引发模式形式)以添加新用户。

The table displays fine, the modal form is raised on the button click, but the submit function tied to the form is never fired upon clicking the submit function. 该表显示正常,单击按钮后会出现模式表单,但是单击提交功能时永远不会触发与表单绑定的提交功能。 Here's an image of the template... 这是模板的图像... 在此处输入图片说明

A user then clicks on "Add Staff" which raises the modal... 然后,用户单击“添加人员”,这将引发模式... 在此处输入图片说明

And after the user fills in the form, the ng-submit function is never being fired. 在用户填写表格后,便永远不会触发ng-submit函数。

Here's the HTML code of the template; 这是模板的HTML代码;

<div data-ng-controller="admController">
    <div class="page-wrapper">
        <div class="banner notdark-translucent-bg" style="background-image:url('assets/images/banner/admin.jpeg'); background-position: 50% 50%;">
            <div class="breadcrumb-container">
                <div class="container">
                     <ol class="breadcrumb">
                        <li class="breadcrumb-item"><i class="fa fa-home pr-2"></i><a class="link-dark" href="home">Home</a></li>
                        <li class="breadcrumb-item active">Administration</li>
                    </ol>
                </div>
            </div>
        </div>

        <section class="main-container">
            <div class="container">
                <div class="row">
                    <div class="main col-lg-8">
                         <h1 class="page-title">Administration</h1>
                         <div class="separator-2"></div>
                    </div>
                </div>
            </div>
        </section>

        <div class="container">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead class="thead-dark">
                        <tr>
                            <th class="text-center">Name</th>
                            <th class="text-center">Address</th>
                            <th class="text-center">Suburb</th>
                            <th class="text-center">Post Code</th>
                            <th class="text-center">Email</th>
                            <th class="text-center">Phone</th>
                            <th class="text-center">  </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="text-center" data-ng-repeat="o in obj">
                            <td>{{o.name}}</td>
                            <td>{{o.address}}</td>
                            <td>{{o.suburb}}</td>
                            <td>{{o.postcode}}</td>
                            <td>{{o.email}}</td>
                            <td>{{o.phone}}</td>
                            <td><input type="radio" name="id" id={{o.id}} value={{o.id}}/></td>
                        </tr>
                    </tbody>
                </table>
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addModal">Add Staff</button>
                <button type="button" class="btn btn-success" data-toggle="modal" data-target="#editModal">Edit Staff</button>
                <button type="button" class="btn btn-secondary" data-toggle="modal" data-target="#deleteModal">Delete User</button>
            </div>
        </div>
    </div>

        <div class="modal" id="addModal" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header text-center">
                <h5 class="modal-title text-center">Add New Staff</h5>
              </div>
              <div class="modal-body">

                        <form data-ng-submit="addstaffform()" name="addstaffform" method="POST">
                          <div class="form-group">
                            <label for="name">Full Name:</label>
                            <input type="text" class="form-control form-control-sm" name="name" id="name" placeholder="">
                          </div>
                          <div class="form-group">
                            <label for="address">Address:</label>
                            <input type="text" class="form-control form-control-sm" name="address" id="address" placeholder="">
                          </div>
                            <div class="form-group">
                                <label for="suburb">Suburb:</label>
                                <input type="text" class="form-control form-control-sm" name="suburb" id="suburb" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="postcode">Post Code:</label>
                                <input type="tel" class="form-control form-control-sm" name="postcode" id="postcode" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="phone">Phone:</label>
                                <input type="tel" class="form-control form-control-sm" name="phone" id="phone" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="email">Email:</label>
                                <input type="email" class="form-control form-control-sm" name="email" id="email" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="suburb">Password:</label>
                                <input type="password" class="form-control form-control-sm" name="password" id="password" placeholder="">
                            </div>
                          <button type="submit" class="btn btn-primary">Submit</button>
                            <button type="reset" value="Reset" class="btn btn-secondry">Clear</button>
                        </form>
              </div>
            </div>
          </div>
        </div>

        <div class="modal" id="editModal" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title text-center">Edit Staff</h5>
                <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>-->
              </div>
              <div class="modal-body">
                        <form ng-submit="submit(editstaffform)" name="editstaffform" method="post" action="" role="form">
                            <div class="form-group">
                                <label for="name">Full Name:</label>
                                <input type="text" class="form-control form-control-sm" name="name" id="name" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="address">Address:</label>
                                <input type="text" class="form-control form-control-sm" name="address" id="address" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="suburb">Suburb:</label>
                                <input type="text" class="form-control form-control-sm" name="suburb" id="suburb" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="postcode">Post Code:</label>
                                <input type="tel" class="form-control form-control-sm" name="postcode" id="postcode" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="phone">Phone:</label>
                                <input type="tel" class="form-control form-control-sm" name="phone" id="phone" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="email">Email:</label>
                                <input type="email" class="form-control form-control-sm" name="email" id="email" placeholder="">
                            </div>
                            <div class="form-group">
                                <label for="suburb">Password:</label>
                                <input type="password" class="form-control form-control-sm" name="password" id="password" placeholder="">
                            </div>
                            <button type="submit" class="btn btn-primary">Submit</button>
                            <button type="reset" value="Reset" class="btn btn-secondry">Clear</button>
                        </form>
              </div>

            </div>
          </div>
        </div>

        <div class="modal" id="deleteModal" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <p>Modal body text goes here.</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary">Save changes</button>
              </div>
            </div>
          </div>
        </div>

</div> <!-- End Controller -->

And here's the Angular Controller 这是角度控制器

app.controller("admController", function ($scope, $http, $location) {
        $(document).ready(function() {
            getStaff();
        });

        $scope.addstaffform = function() {
            $scope.formData = {};
                $http({
                    method  : 'POST',
                    url     : 'assets/php/addStaff.php',
                    data    : $.param($scope.formData),  //param method from jQuery
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
                }).success(function(data){
                    console.log(data);
                    if (!data.success) { //success comes from the return json object

                        $scope.result='bg-success';
                    } else {
                        $('#addModal').modal('hide');
                        getStaff();

                        $scope.result='bg-danger';
                    }
                }).error(function(data){
                    console.log(data);
                });
        };

        function getStaff() {}
            $http.get('assets/php/admin.php')
                    .then(
                        function (response) {
                            $scope.obj = response.data;
                        },
                        function (response) {
                            // error handling routine
                        }
                    );
                });

so in debugging i've found that $scope.addstaffform = function() is being executed on the template loading, but its never fired when clicking on the submit button. 因此,在调试中,我发现$scope.addstaffform = function()正在模板加载时执行,但是在单击$scope.addstaffform = function()按钮时从未触发过。

 var app = angular.module('myApp', []); app.controller("admController", function ($scope, $http, $location) { $scope.addstaffform = function() { alert("hello"); } }); 
 <!DOCTYPE html> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> <body ng-app="myApp"> <div data-ng-controller="admController"> <div class="page-wrapper"> <div class="banner notdark-translucent-bg" style="background-image:url('assets/images/banner/admin.jpeg'); background-position: 50% 50%;"> <div class="breadcrumb-container"> <div class="container"> <ol class="breadcrumb"> <li class="breadcrumb-item"><i class="fa fa-home pr-2"></i><a class="link-dark" href="home">Home</a></li> <li class="breadcrumb-item active">Administration</li> </ol> </div> </div> </div> <section class="main-container"> <div class="container"> <div class="row"> <div class="main col-lg-8"> <h1 class="page-title">Administration</h1> <div class="separator-2"></div> </div> </div> </div> </section> <div class="container"> <div class="table-responsive"> <table class="table table-striped table-hover"> <thead class="thead-dark"> <tr> <th class="text-center">Name</th> <th class="text-center">Address</th> <th class="text-center">Suburb</th> <th class="text-center">Post Code</th> <th class="text-center">Email</th> <th class="text-center">Phone</th> <th class="text-center"> </th> </tr> </thead> <tbody> <tr class="text-center" data-ng-repeat="o in obj"> <td>{{o.name}}</td> <td>{{o.address}}</td> <td>{{o.suburb}}</td> <td>{{o.postcode}}</td> <td>{{o.email}}</td> <td>{{o.phone}}</td> <td><input type="radio" name="id" id={{o.id}} value={{o.id}}/></td> </tr> </tbody> </table> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addModal">Add Staff</button> <button type="button" class="btn btn-success" data-toggle="modal" data-target="#editModal">Edit Staff</button> <button type="button" class="btn btn-secondary" data-toggle="modal" data-target="#deleteModal">Delete User</button> </div> </div> </div> <div class="modal" id="addModal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header text-center"> <h5 class="modal-title text-center">Add New Staff</h5> </div> <div class="modal-body"> <form data-ng-submit="addstaffform()" name="addstaff" > <div class="form-group"> <label for="name">Full Name:</label> <input type="text" class="form-control form-control-sm" name="name" id="name" placeholder=""> </div> <div class="form-group"> <label for="address">Address:</label> <input type="text" class="form-control form-control-sm" name="address" id="address" placeholder=""> </div> <div class="form-group"> <label for="suburb">Suburb:</label> <input type="text" class="form-control form-control-sm" name="suburb" id="suburb" placeholder=""> </div> <div class="form-group"> <label for="postcode">Post Code:</label> <input type="tel" class="form-control form-control-sm" name="postcode" id="postcode" placeholder=""> </div> <div class="form-group"> <label for="phone">Phone:</label> <input type="tel" class="form-control form-control-sm" name="phone" id="phone" placeholder=""> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control form-control-sm" name="email" id="email" placeholder=""> </div> <div class="form-group"> <label for="suburb">Password:</label> <input type="password" class="form-control form-control-sm" name="password" id="password" placeholder=""> </div> <button type="submit" class="btn btn-primary" >Submit</button> <button type="reset" value="Reset" class="btn btn-secondry">Clear</button> </form> </div> </div> </div> </div> <div class="modal" id="editModal" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title text-center">Edit Staff</h5> <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button>--> </div> <div class="modal-body"> <form ng-submit="submit(editstaffform)" name="editstaff" method="post" action="" role="form"> <div class="form-group"> <label for="name">Full Name:</label> <input type="text" class="form-control form-control-sm" name="name" id="name" placeholder=""> </div> <div class="form-group"> <label for="address">Address:</label> <input type="text" class="form-control form-control-sm" name="address" id="address" placeholder=""> </div> <div class="form-group"> <label for="suburb">Suburb:</label> <input type="text" class="form-control form-control-sm" name="suburb" id="suburb" placeholder=""> </div> <div class="form-group"> <label for="postcode">Post Code:</label> <input type="tel" class="form-control form-control-sm" name="postcode" id="postcode" placeholder=""> </div> <div class="form-group"> <label for="phone">Phone:</label> <input type="tel" class="form-control form-control-sm" name="phone" id="phone" placeholder=""> </div> <div class="form-group"> <label for="email">Email:</label> <input type="email" class="form-control form-control-sm" name="email" id="email" placeholder=""> </div> <div class="form-group"> <label for="suburb">Password:</label> <input type="password" class="form-control form-control-sm" name="password" id="password" placeholder=""> </div> <button type="submit" class="btn btn-primary">Submit</button> <button type="reset" value="Reset" class="btn btn-secondry">Clear</button> </form> </div> </div> </div> </div> <div class="modal" id="deleteModal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <p>Modal body text goes here.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> </div> </body> </html> 

just change your form name and function name means these should be different or these should be differentiable to each other.This is the only possible solution I get . 只需更改您的表单名和函数名就意味着它们应该不同或彼此可以区分。这是我得到的唯一解决方案。

Having the form name and function name the same was the problem; 问题在于表单名称和函数名称相同。

But then i also found another problem with the ajax call ~ due to changes in version 1.6 但是后来我也发现ajax调用还有另一个问题〜由于版本1.6的更改

Old code: 旧代码:

$http({
                    method  : 'POST',
                    url     : 'assets/php/addStaff.php',
                    data    : $.param($scope.formData),  //param method from jQuery
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
                }).success(function(data){
                    console.log(data);
                    if (!data.success) { //success comes from the return json object

                        $scope.result='bg-success';
                    } else {
                        $('#addModal').modal('hide');
                        getStaff();

                        $scope.result='bg-danger';
                    }
                }).error(function(data){
                    console.log(data);
                });

the "success" and "error" callbacks have been deprecated, and replaced with "then" and "catch". 已不赞成使用“成功”和“错误”回调,并将其替换为“然后”和“捕获”。
New Code: 新代码:

$http({
                    method  : 'POST',
                    url     : 'assets/php/addstaff.php',
                    data    : $.param($scope.formData),  //param method from jQuery
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
                }).then(function (response) {
                    console.log(response.data);
                    ans = response.data;
                    if (!ans.success) { //success comes from the return json object

                        $scope.result='bg-success';
                    } else {
                        $('#addModal').modal('hide');
                        getStaff();

                        $scope.result='bg-danger';
                    }
                }).catch(function(response){
                    console.log(response.data);
                });

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

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