[英]ng-submit not working in angularjs
我的看法:
<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()">×</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>
我的控制器:
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);
});
}
}]);
然而,当我点击保存时,我没有得到警报。 不知道那里发生了什么。
即使按钮位于表单内且仍然无法使用ng-submit
更改您的按钮type
以进行提交 。
<button type="submit" class="btn btn-success" ng-click="login()">Login</button>
PS:在我更改按钮类型submit
之前,其他答案对我没有帮助。 希望这可以帮助。
也许你应该在表单标签上添加'novalidate'属性,以避免本地浏览器验证,如https://docs.angularjs.org/guide/forms
这也会影响,表单中必须有一个提交按钮(按钮或输入类型=“提交”)或ng-submit不起作用。
对我来说,问题是其中一个表单字段上的'required'标记。 删除此标记后,表单再次起作用。 解决此类问题的旧方法是尝试对表单的一部分进行注释并运行代码。
我更喜欢在表单上使用'novalidate'标签,使用type =“submit”的按钮,以便通过angular完成表单的验证。
最愚蠢的错误,确保你正在编译你的js代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.