gpt4 book ai didi

javascript - 发送带 Angular 表格

转载 作者:行者123 更新时间:2023-11-30 12:16:20 25 4
gpt4 key购买 nike

我有这样的angularJs脚本

var formApp = angular.module('formApp', []);
formApp.controller('formController', ['$scope', '$http', function($scope, $http) {
$scope.formData = {};
$scope.processForm = function() {
$http({
method : 'POST',
url : $('form').attr('action'),
data : $scope.formData,
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
})
.success(function(data) {
console.log(data);
if (!data.success) {
$scope.errorName = data.errors.name;
$scope.errorSuperhero = data.errors.superheroAlias;
} else {
$scope.message = data.message;
$scope.errorName = '';
$scope.errorSuperhero = '';
}
});
};
}]);

在我的 html 中

<div ng-app="formApp" ng-controller="formController" class="container">
<div class="row">
<div class="col-lg-8 center-block">
<form action="/todo" ng-submit="processForm()">
<label for="login">Login</label>
<input ng-model="formData.login" id="login" type="text" name="" class="form-control" />
<label for="pass">Password</label>
<input ng-model="formData.pass" id="pass" type="password" name="" class="form-control" />
<label for="email">Email</label>
<input ng-model="formData.email" id="email" type="email" name="" class="form-control" />
<input ng-click="Submit" class="btn btn-primary" type="button" value="Register now" />
</form>
</div>
</div>
</div>

但结果我的表单没有发送。什么都没发生。我不知道做错了什么。请帮我解决这个问题

最佳答案

如果您想使用 ng-submit,请将您的按钮输入替换为如下内容:

<input type="submit" id="submit" value="Register now" />

不过,我建议删除表单元素上的 ng-submit 并将输入按钮修改为:

<input ng-click="processForm()" class="btn btn-primary" type="button" value="Register now" />

这两种方法都可以,但您不应该尝试将提交按钮上的 ng-click 和表单上的 ng-submit 组合起来。

请参阅 ng-submit 的 Angular 文档: https://docs.angularjs.org/api/ng/directive/ngSubmit

关于javascript - 发送带 Angular 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com