AngularJS ng-submit函数未执行

时间:2018-07-02 07:15:00

标签: javascript jquery html angularjs angularjs-controller

我正在开发一个AngularJS项目,并且我有一个显示用户表的模板,然后有一个按钮(引发模式形式)来添加新用户。

该表显示正常,单击按钮后将出现模式表单,但是单击该提交功能时永远不会触发与该表单绑定的提交功能。 这是模板的图像... enter image description here

然后,用户单击“添加人员”,这将引发模式... enter image description here

在用户填写表格后,便不会触发ng-submit函数。

这是模板的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 -->

这是Angular控制器

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
                        }
                    );
                });

因此在调试中,我发现$scope.addstaffform = function()是在模板加载时执行的,但单击提交按钮时从未触发过。

2 个答案:

答案 0 :(得分:1)

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>

只需更改您的表单名和函数名,就意味着它们应该互不相同或互不相同。这是我得到的唯一可能的解决方案。

答案 1 :(得分:0)

表单名称和函数名称相同是问题所在;

但是后来我也发现ajax调用存在另一个问题〜由于版本1.6的更改

旧代码:

$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);
                });

“成功”和“错误”回调已被弃用,并已替换为“然后”和“抓住”。
新代码:

$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);
                });