简体   繁体   中英

AngularJS function not invoked by ng-submit button click

I have a bootstrap modal with a form and the submit but will not invoke a function defined inside my AngularJS controller. I am able to read and display the data on the page but when I click the submit nothing happens. It just closes. For testing I am trying to console.log a message upon clicking the submit button. Thank you.

AngularJS:

var app = angular.module('myApp', []);

app.controller('supportController',
    function($scope, $http) {

        $scope.updateList = function() {
            console.log('updateList function invoked!');
        };

        $http({
            method: 'GET',
            url: ".../_api/web/lists/GetByTitle('DCO-%20IT%20Issue%20Tracker')/items?",
            headers: {"Accept": "application/json;odata=verbose"}
        }).then(function (data, status, headers, config) {
            $scope.tickets = data.data.d.results;
            $scope.tickets.map(ticket => console.log(ticket));

    }, function errorCallback(response) {
        console.log(response);
    });
});

HTML:

<div class="container" ng-controller="supportController">
        <div class="row">
            <div class="col-12">
                <button class="btn btn-secondary btn-block" data-toggle="modal" data-target="#myModal">Submit Issue/Request Ticket</button>
            </div>
        </div>
        <div class="row">
            <div class="col-12">
                <table id="searchTextResults" class="table table-striped">
                    <tr>
                        <th>Title</th>
                        <th>Network</th>
                        <th>Details</th>
                    </tr>
                    <tr ng-repeat="ticket in tickets">
                        <td>{{ticket.Title}}</td>
                        <td>{{ticket.Network}}</td>
                        <td>{{ticket.Details}}</td>
                    </tr>
                </table>
            </div>
        </div>
        <!-- The Modal -->
        <div class="modal" id="myModal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <!-- Modal Header -->
                    <div class="modal-header">
                        <h4 class="modal-title">DISA Europe - EU34 Support Request</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <!-- Modal body -->
                    <div class="modal-body">
                        <form action="" method="POST" ng-submit="updateList()">
                            <div class="form-group">
                                <!-- <label>Title</label> -->
                                <div>
                                    <lable>Submitted By</lable>
                                    <div id="user"></div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Title</label>
                                <div>
                                    <input type="text" class="form-control input-lg" name="title" id="title">
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Network</label>
                                <div>
                                    <input type="text" class="form-control input-lg" name="network" id="network">
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Details</label>
                                <div>
                                    <input type="text" class="form-control input-lg" name="details" id="details">
                                </div>
                            </div>
                        </form>
                    </div>
                    <!-- Modal footer -->
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" data-dismiss="modal">Submit</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

I think there are 2 things you need to refine in your template to make it work accordingly:

  • Wrap the <button type="submit" /> within the <form />

  • Remove the action attribute out of your <form /> to prevent the default action of submit + reload the page.

<form ng-submit="updateList()">
  // ...
  <!-- Modal footer -->
  <div class="modal-footer">
      <button type="submit" class="btn btn-primary" data-dismiss="modal">Submit</button>
      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  </div>
</form>

删除提交按钮的 data-dismiss 属性允许调用该函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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