ng-submit不在angularjs中工作

Sha*_*oon 24 javascript angularjs ng-submit

我的看法:

<div class="modal" tabindex="-1" role="dialog" ng-controller="LocationController">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" ng-click="$hide()">&times;</button>
        <h4 class="modal-title">
          Add a Location
        </h4>
      </div>
      <div class="modal-body">
        <form class="form-horizontal" role="form" ng-submit="createLocation()">
          <div class="form-group">
            <label class="col-sm-2 control-label">Name</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" placeholder="Warehouse A, Row 15, Shelf BC1, etc" ng-model="name">
            </div>
          </div>

          <div class="form-group">
            <label class="col-sm-2 control-label">Type</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" placeholder="warehouse, row, shelf, etc" ng-model="type">
            </div>
          </div>

        </form>
      </div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Save</button>
        <button type="button" class="btn btn-danger" ng-click="$hide()">Cancel</button>
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

angular.module('mean').controller('LocationController', ['$scope', '$location', '$rootScope', 'LocationService', '$modal', '$routeParams', function ($scope, $location, $rootScope, LocationService, $modal, $routeParams) {

  $scope.createLocation = function() {
alert('afds');
    LocationService.create(this).then(function(response) {
      console.log(response);
    });
  }

}]);
Run Code Online (Sandbox Code Playgroud)

然而,当我点击保存时,我没有得到警报.不知道那里发生了什么.

Sha*_*oon 41

感谢Matt Way评论 - 结果我的保存按钮不在我的表格之内.固定

  • Blegh,我有同样的问题(单击表单提交按钮时没有调用函数),我的提交按钮肯定在我的表单中. (10认同)
  • OP回答了关于自我评论的链接.这是迄今为止我见过的最有趣的自我指导的答案.再加上它解决了我的问题! (2认同)

Ama*_*ath 22

即使按钮位于表单内部仍然ng-submit无法正常工作,请更改按钮type进行提交.

<button type="submit" class="btn btn-success" ng-click="login()">Login</button>
Run Code Online (Sandbox Code Playgroud)

PS:在我将按钮类型更改为之前,其他答案对我没有帮助submit.希望这可以帮助.


Mus*_*sma 7

也许你应该在表单标签上添加'novalidate'属性,以避免本地浏览器验证,如https://docs.angularjs.org/guide/forms