我有一些形式的模式,我創建對象存儲到數據庫中。 在這些形式中我有一個選擇場這樣的:HTML通過ajax選擇字段調用Web服務
<div class="form-group" id=existingUser>
<label>Username</label> <select class="form-control select2"
style="width: 100%;" th:field="*{user}">
<option th:each="user: ${users}" th:value="${user.username}"
th:text="${user.username}"></option>
</select>
</div>
,用戶從控制器通過與模型。使用這種方法,我必須更新所有頁面以刷新選擇字段中的值,否則我只能更新表格,在其中顯示新創建的對象,但我不能在select中使用新字段。 的問題是性能和外觀清爽的頁面,而且我不能使用這些指令顯示我的消息
location.reload();
//reload only the tag with id carsTable, so only the table
//$('#carsTable').load(document.URL + ' #carsTable');
$('#addCarModal').modal("hide");
notifyMessage("Your car has been created!", 'success');
function notifyMessage(textMessage, typeMessage){
$.bootstrapGrowl(textMessage, {
type: typeMessage, // (null, 'info', 'error', 'success')
});
}
有沒有一種方法來調用Ajax時模式被稱爲?或者我可以將數據從JavaScript傳遞給HTML(如果我在點擊添加按鈕時檢索值)。 有時我也檢查是否選擇字段是空的,在這種情況下顯示在模式內的消息而不是形式。感謝
UPDATE。我thouth這段代碼:我在html頁面
function freeUserController($scope) {
$http.get("https://localhost:8080/users/")
.success(function(data) {
$scope.users = data;
});
}
:
要開始只有成功管理
<div ng-controller="freeUserController" class="form-group" id=existingUser>
<label>Username</label> <select class="form-control select2"
style="width: 100%;" name="user">
<option ng-repeat="user in users" value="{{user.username}}">
{{user.username}}</option>
</select>
</div>
解決方法可能是使用JavaScript來編寫HTML,如你的例子,但我會有很多回復的代碼。 我從來沒有使用angularjs,但我知道它可以創建一個動態綁定,我可以插入ajax調用到angularjs控制器? – luca
是的,AngularJS有很多AJAX功能。它甚至有它自己的AJAX函數包裝:https://docs.angularjs.org/api/ng/service/$http – KWeiss
我更新可能的代碼來刷新選擇字段,這是正確的方式?它如何知道什麼時候刷新? – luca