javascript - 使用 AngularJS 发布请求

标签 javascript angularjs ajax

我有以下html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SPA book_store</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope, $http) {
            $http.get("http://localhost:8080/book_store/rest/books_json/get")
                    .then(function (response) {
                        $scope.books = response.data;
                    });
            $(document).ready(function () {
                $('#call').click(function () {
                    $.ajax({
                        type: "post",
                        url: "http://localhost:8080/book_store/rest/books_json",
                        data: $('#buyBookForm').serialize(),
                        success: function (response) {
                            $scope.books = response.data;
                        }
                    });
                });
            });
        });

    </script>
</head>
<body>


<div class="container" ng-app="myApp" ng-controller="myCtrl">
    <h1>Book Store</h1>
    <p>Choice any book you like:</p>

    <form id="buyBookForm" method="post">
        <table id="table" class="table">
            <thead>
            <tr>
                <th>Book Name</th>
                <th>Author</th>
                <th>Genre</th>
                <th>Price</th>
                <th>Sold</th>
                <th>Bought By</th>
            </tr>
            </thead>
            <tbody>

            <input id="filter_input" type="text" ng-model="nameText"/>
            <ul>
                <tr ng-repeat="book in books | filter:nameText | orderBy:'name'">
                        <td>
                            <input type="checkbox" name="book{{book.name}}"
                                   value="{{book.book_id}}"> <label>{{book.name}}</label>
                        </td>
                        <td>{{book.author.name}}</td>
                        <td>{{book.genre}}</td>
                        <td>{{book.price}}</td>
                        <td>{{book.bought}}</td>
                        <td>{{book.buyCount}}</td>
                </tr>
            </ul>
            </tbody>
        </table>

    </form>
    <input type="submit" name="submit" value="Purchase" id="call">
</div>

</body>
</html>

它工作正常,但当我调用“购买”时,它不会重新加载book模型。所以我必须调用浏览器刷新才能看到更改。

问题: 如何让我的模型在点击“购买”后自动更新值?

最佳答案

发生这种情况是因为您使用的是 jQuery 而不是 Angular。

将脚本更改为

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {


   $http.get("http://localhost:8080/book_store/rest/books_json/get")
       .then(function (response) {
           $scope.books = response.data;
    });

   $scope.post = function(){
      $http.post("http://localhost:8080/book_store/rest/books_json", $('#buyBookForm').serialize())
       .then(function (response) {
           $scope.books = response.data;
       });
   }


});
</script>

我定义了一个名为 post 的作用域函数,它对您的服务器进行 $http 调用。

然后,使用 ng-click 调用 post。将按钮更改为

<input type="submit" ng-click="post()" name="submit" value="Purchase" id="call">
<小时/>

编辑

我做了一些更改,因为这是一个不同的调用。我还建议将 ng-models 添加到 buyBookForm,这样您就可以删除 jQuery。

关于javascript - 使用 AngularJS 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38770365/

相关文章:

javascript - 通过 for 循环 + jquery 显示数组中的所有项目

javascript - 不使用 var、let 或 const 的对象解构

javascript - 为什么在 If 中的 someArray 中做 someVar 总是给出假值

node.js - 计算 mongodb 中不同值的数量,然后计算总和

javascript - 编译 ng-bind-html 后 ng-click 不工作

javascript - 设置从服务器端加载的后退按钮的下拉值

javascript - 在没有页面刷新的情况下提交表单不起作用

javascript - 单击图标时,带有 glyphicon 的 onclick div 不起作用

javascript - Parse Cloud - 删除属性(指针)

javascript - HTML 表单方法与 jQuery 函数类型