【问题标题】:AngularJS function not invoked by ng-submit button clickng-submit 按钮单击未调用 AngularJS 函数
【发布时间】:2020-09-16 07:24:49
【问题描述】:

我有一个带有表单和提交的引导模式,但不会调用在我的 AngularJS 控制器中定义的函数。我能够在页面上读取和显示数据,但是当我单击提交时没有任何反应。它只是关闭。为了进行测试,我试图在单击提交按钮时 console.log 一条消息。谢谢。

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>

【问题讨论】:

  • 尝试从您的表单中删除 action=""

标签: javascript angularjs


【解决方案1】:

我认为您需要在模板中改进两点以使其相应地工作:

  • 将&lt;button type="submit" /&gt; 包裹在&lt;form /&gt; 中

  • 从&lt;form /&gt; 中删除action 属性,以防止提交+重新加载页面的默认操作。

<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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多