當我的表單提交了ng-submit =「createNewOrder()」時,我調用一個執行服務器端驗證的資源服務。但是,服務返回後,表單域將被清除。如何阻止Angular清除字段,直到資源服務返回表示成功的響應?AngularJS - 防止表單清除
編輯:添加代碼
HTML:
<div ng-app="OrdersApp" ng-controller="OrdersController" class="ng-scope">
<form name="newOrderForm" ng-submit="createNewOrder();" novalidation>
<label>First Name</label>
<input type="text" ng-model="newOrder.firstName" required>
<label>Last Name</label>
<input type="text" ng-model="newOrder.lastName" required>
etc.
</form>
</div>
應用:
var app = angular.module('OrdersApp', ['orderServices']);
控制器:
app.controller('OrdersController', function ($scope, Order) {
$scope.newOrder = new Order({ });
$scope.createNewOrder = function() {
$scope.newOrder.$create(
function(successData) {
if (successData.error) return alert('order was not saved successfully');
alert('order successfully charged and created');
},
function(errorData) {
alert('order was not saved successfully: ' + JSON.stringify(errorData));
}
);
};
});
服務:
angular.module('orderServices', ['ngResource']).
factory('Order', function ($resource) {
return $resource('/admin/json/orders', {},
{
query: { method: 'GET', params: { }, isArray: true },
create: { method: 'POST', params: { } },
save: { method: 'PUT', params: { } }
});
});
編寫代碼,我也有一個表單,沒有什麼清楚的,所以也許這是代碼中的錯誤。 – mautrok
看到這個答案 - http://stackoverflow.com/questions/16153313/saving-new-models-using-angularjs-and-resource/33991999#33991999 – Vivek