javascript - 如何使用 AngularJS 保存输入值

标签 javascript java html angularjs

我正在开发一个网站,其中包含一个名为评论的字段,某些特定用户可以在其中发布这些评论。 我的 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>
这是 Controller 功能:

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/

相关文章:

JavaScript MVC : how do views notify the controller

javascript - Polymer 1.2.4 paper-progress 和 iron-image 有疯狂的 border-radius

javascript - 为什么我的数组要重组?

javascript - 如何在文章网站的特定行上创建弹出窗口?

java - Web应用的Hibernate session 连接关系

javascript - 使用 javascript 编译 html 并将其转储到文件

java - 我应该在 Wildfly 中对 EJB 2.1 session bean 使用 PortableRemoteObject.narrow()

java表达式太复杂减少条件运算符的数量

python - 如何获取特定时间范围的平均值

html - 在页面末尾生成不需要的空白