我正在开发一个网站,其中包含一个名为评论的字段,某些特定用户可以在其中发布这些评论。 我的 HTML 代码:
<form>
<input ng-model="stack"></input>
<button ng-click="save()">Save</button>
<p>Your comment: <span ng-bind="stack"></span></p>
</form>
注释应该保存到一个变量中,然后发布到我的 Web 服务中,该服务是一个 json 文件,以便我可以进行数据库调用。我必须使用 $http.post 我正在使用 AngularJS,并且我已经使用 java (JAXB) 编写了我的 Web 服务。
//Controller:
$scope.save = function() {
alert(name);
}
//In save I put alert to test because all the functions save I tested hadn't done what I need.
//server
$http({
method: 'POST',
url: 'url',
data: "stack="
});
最佳答案
$http.post
为您完成。
这是 html 代码:
<form ng-submit="save()">
<input ng-model="stack"></input>
<button type="submit">Save</button>
<p>Your comment:<span ng-bind="stack"></span></p>
</form>
function myController($scope,$http){
$scope.save=function(){
var data=$scope.stack;
/* post to server*/
$http.post(url, data)
.then(
function(response){
// success callback
},
function(response){
// failure callback
});
}
}
关于javascript - 如何使用 AngularJS 保存输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37541212/