如何使用AngularJS保存输入值 [英] How to save an input value using AngularJS

查看:64
本文介绍了如何使用AngularJS保存输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上工作,该网站包含一个名为评论的字段,其中某些特定用户可以发布这些评论。
我的HTML代码:



< form> < input ng-model =stack>< / input> <按钮ng-click =save()>保存< /按钮> < p>您的评论:< p>您的评论:< span ng-bind =stack>< / span>< / p>< / form>< / code&

这些注释应该保存到一个变量中,然后发布在我的web服务中,这是一个json文件,这样我就可以进行数据库调用。我必须使用$ http.post
我正在使用AngularJS,并使用java(JAXB)编写了我的webservice。

} / /保存我把警报测试,因为我测试保存的所有功能没有做我所需要的。 // server $ http({method:'POST',url:'url',data:stack =});

解决方案

$ http.post >

这是html代码:

< form ng -submit = 保存() > < input ng-model =stack>< / input> <按钮类型=提交>保存< /按钮> < p>您的评论:< p>您的评论:< span ng-bind =stack>< / span>< / p>< / form>< / code&
这是控制器函数:

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}); }}


I am working on a website that contains a field called comments where some particular users can post those comments. My HTML code:

<form>
  <input ng-model="stack"></input>
  <button ng-click="save()">Save</button>
  <p>Your comment:  <span ng-bind="stack"></span></p>
</form>

The comments should be saved in to a variable then, posted in my web service which is a json file so I can do my Database calls. I have to use $http.post I am using AngularJS and I have written my webservice using java (JAXB).

//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 does it for you.

This is the html code :

<form ng-submit="save()">
      <input ng-model="stack"></input>
      <button type="submit">Save</button>
      <p>Your comment:<span ng-bind="stack"></span></p>
</form>

This is the controller function :

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
               });
                               
             }
        }

这篇关于如何使用AngularJS保存输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆