Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 我想在使用web API和angular resource创建新记录时获取新ID_Angularjs_Ngresource - Fatal编程技术网

Angularjs 我想在使用web API和angular resource创建新记录时获取新ID

Angularjs 我想在使用web API和angular resource创建新记录时获取新ID,angularjs,ngresource,Angularjs,Ngresource,我正在使用Web API和Angular资源来管理我的应用程序。。 在控制器中使用保存方法时,我希望接收新保存的记录ID My web API方法在响应创建消息中返回新ID。。但是我看不懂 这是我的服务: 'use strict'; appServices.factory('projectsService', function ($resource, ngAuthSettings) { var serviceBase = ngAuthSettings.apiServiceBaseUri;

我正在使用Web API和Angular资源来管理我的应用程序。。 在控制器中使用保存方法时,我希望接收新保存的记录ID

My web API方法在响应创建消息中返回新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;

有什么想法吗???

您很可能被从api传回一个json对象。因此,newid可能是一个json对象。尝试更新此行:

$location.path('/project/' + newid);

另外,确保您的api在响应中真正传回了id,如果上面的行不适合您,请检查id字段的属性名称

更新:

尝试更新web.api响应函数,使其如下所示:

return Request.CreateResponse(HttpStatusCode.Created, new { projectRepository.NewID });

注意:去掉jQuery。这真的把你搞砸了。您永远不应该在控制器中有DOM引用,对于angular,您可能根本不需要使用DOM选择器。看起来您需要在此处使用angular的表单/验证。谢谢你的评论,我使用的JQuery是用于欧芹valdiation。。它比Angular提供了更好的简单有效性。。。我希望我能找到更好的。。。它甚至还有助于ajax验证…感谢您的响应,,,您能告诉我如何将json对象与响应一起传递回去吗??该对象已通过返回以下内容将ID作为松散值返回:HttpResponseMessage response=Request.CreateResponseHttpStatusCode.Created,projectRepository.NewID;返回响应;projectRepository.NewID保留已保存对象的新ID。。如何将其放入ajax对象中我真的不知道:我找到了一些东西,它返回一个新id的数组,如下所示:如果id为34,它将重新命名{3',4'}。。。或者,如果我返回一个类似test的字符串,它返回{t',e',s',t},那么毕竟我向响应传递了错误的值,那么如何传递righ值???看看我更新的答案。对您的web.api进行建议的修改,并让我知道它是如何为您工作的。您的问题解决了吗?如果你需要其他帮助,请告诉我。
return Request.CreateResponse(HttpStatusCode.Created, new { projectRepository.NewID });