gpt4 book ai didi

javascript - 如何在 AngularJS 中使用三个输入来 POST 表单

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

我在一个表单中有 3 个输入。

Name:
Class:
Subject:

我必须接受输入,调用 API 并执行像

这样的发布请求
{"Name":"hi","Class":"B","Subject":c}

如何在 AngularJS 中做到这一点?

最佳答案

index.html

<!DOCTYPE html>
<html>

<head>
<link data-require="bootstrap@4.0.5" data-semver="4.0.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
<script data-require="bootstrap@4.0.5" data-semver="4.0.5" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js"></script>
<script data-require="angularjs@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script src="script.js"></script>
</head>

<body class="container" ng-app="myApp" ng-controller="formController" ng-cloak>
<!--Form-->
<form ng-submit="formData()">
<div class="form-group">
<label class="control-label col-md-2">Name</label>
<div class="col-md-4">
<input type="text" class="form-control" ng-model="data.name"" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Class</label>
<div class="col-md-4">
<input type="text" class="form-control" ng-model="data.class"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2">Subject</label>
<div class="col-md-4">
<input type="text" class="form-control" ng-model="data.subject"/>
</div>
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="submit" class="btn btn-success btn-sm" ng-show="submit" />
</div>
</form>
<!--End Form-->

</body>

</html>

脚本.js

// Code goes here
var myApp = angular.module('myApp', []);
myApp.controller('formController', ['$scope', '$http', function($scope, $http) {

//Buttons Settings
$scope.submit = true;


//form data manipulate
$scope.formData = function() {

//$http POST function
$http({

method: 'POST',
url: 'https://demoapi.com/formdata',
data: $scope.data

}).then(function successCallback(response) {

alert("created Successfully")

}, function errorCallback(response) {

alert("Error. while created user Try Again!");

});

};


}]);

关于javascript - 如何在 AngularJS 中使用三个输入来 POST 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59465242/

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