【发布时间】:2015-03-02 23:07:35
【问题描述】:
我正在使用 Web API 和 Angular 资源来管理我的应用程序.. 在我的控制器中使用 Save 方法时,我想接收新保存的记录 ID...
我的 Web API 方法在响应 Created Message 中返回新 ID。但我无法阅读它...
这是我的服务:
'use strict';
appServices.factory('projectsService', function ($resource, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
return $resource(serviceBase + '/api/project/:id', {}, {
get: { method: 'GET', params: { id: '' }, isArray: true },
getById: { method: 'GET', params: { id: '0' }, isArray: false },
save: { method: 'POST' ,isArray: false},
update: { method: 'PUT' },
remove: { method: 'DELETE' }
});
});
这是我的控制器方法:
$scope.save = function ($event) {
$event.preventDefault();
$('#AddProjectForm').parsley().validate();
if ($('#AddProjectForm').parsley().isValid() ) {
//HEre is my save method
projectsService.save($scope.project).$promise.then(function (newid) {
$location.path('/project/' + newid);
},
function (response) {
$scope.message = "Failed to register user due to:" + errors.join(' ');
});
}
};
在我的 Web API 中,这是返回
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, projectRepository.NewID);
return response;
有什么想法吗??
【问题讨论】:
-
请注意:摆脱 jQuery。真是把你搞砸了。您永远不应该在控制器中有 DOM 引用,并且使用 Angular,您可能根本不需要使用 DOM 选择器。看起来您需要在这里使用 Angular 的表单/验证。 docs.angularjs.org/guide/forms
-
感谢您的评论,我使用的 JQuery 是用于 Parsley valdiation.. 它提供了比 angualr 更好的简单验证...我希望我能找到任何更好的角度验证...甚至还有助于 ajax 验证...
标签: angularjs ngresource