Ich habe folgende html
:Gesuch mit AngularJS
<!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>
Es funktioniert gut, aber wenn ich „Kauf“ nennen, ist es nicht book
Modell neu zu laden. Ich muss den Browser aktualisieren, um Änderungen zu sehen.
Frage: Wie kann ich mein Modell Autoupdate-Werte nach dem Klicken auf "Kaufen" machen?
Verwendung '$ http', die anstelle von' .ajax', die nicht ist $ Winkel bewusst ist. – Claies
Verwenden Sie auch 'ng-click' anstelle des bindenden jQuery-Stils, klicken Sie, wenn möglich, auf Event-Handler. – Claies
Wenn Sie Angular verwenden, beenden Sie die Verwendung von jQuery. Laden Sie nicht einmal die Bibliothek, also müssen Sie herausfinden, wie Sie es machen können. Vertrauen Sie mir: Sobald Sie lernen, ist es viel einfacher. Wenn Sie jQuery verwenden, wird das Modell nicht aktualisiert, und Sie erhalten endlos viele $ timeouts und $ scope. $ Apply() 's –