0
我有一個網站,根據表單提交將標記添加到地圖。提交表單後,直到用戶刷新頁面纔會顯示標記。因此,我試圖使它在點擊提交後自動刷新頁面。有很多這樣的問題,但都包括PHP,我不知道如何將它們應用於我的代碼。下面是我目前:提交表單後刷新頁面
(1)提交的表單JS文件時
function mainController($scope, $http) {
$scope.formData = {};
$http.get('/api/events')
.success(function(data) {
$scope.events = data;
initMap(data);
for(i = 0; i < data.length; i++){
console.log(data[i].eventLocation);
}
})
.error(function(data) {
console.log('Error: ' + data);
});
// when submitting the add form, send the text to the node API
$scope.createEvent = function() {
$http.post('/api/events', $scope.formData)
.success(function(data) {
$scope.formData = {}; // clear the form so our user is ready to enter another
$scope.events = data;
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
};
$scope.validateForm = function() {
if (!$scope.formData.eventName) {
alert("Please give your event a name!");
return false;
}
else if (!$scope.formData.eventType) {
alert("Please choose an event type!");
return false;
}
else if (!$scope.formData.eventLocation) {
alert("Please choose a location!");
return false;
}
else if (!$scope.formData.eventDetails) {
alert("Please include some details about your event!");
return false;
}
return true;
}
}
(2)我的HTML表單文件
<!-- Event form -->
<div class="col-lg-6">
<!-- Validate form -->
<form name="myForm" ng-submit="validateForm()">
<div class="form-group">
<label>Event Name</label>
<input type="text" name="inputName" class="form-control" ng-model="formData.eventName" placeholder="Event name">
</div>
<div class="form-group">
<label>Type</label>
<select class="form-control" id="inputType" ng-model="formData.eventType">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
<div class="form-group">
<label>Location</label>
<select class="form-control" id="inputLocation" ng-model="formData.eventLocation">
<option>Location 1</option>
<option>Location 2</option>
<option>Location 3</option>
</select>
</div>
<div class="form-group">
<label>Event Details</label>
<textarea class="form-control" name="inputDetails" ng-model="formData.eventDetails" rows="2" placeholder="Add details about your event"></textarea>
</div>
<div class="text-center">
<button id="add-event"type="submit" class="btn btn-primary" ng-click="createEvent()">Submit</button>
</div>
</form>
你爲什麼要使用Ajax而不是常規的形式提交? Ajax的重點在於避免加載新頁面。 – Quentin
window.location = window.location.href – MasNotsram
@Quentin你可以給一個關於如何簡單地重新加載谷歌地圖的建議嗎? – user2990