[英]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.