我在AngularJS中有一个非常简单的应用程序,它使用完整的REST(风帆)后端。
在我的services.js中,我宣布了一个像
这样的资源MyServices.factory('Post', ['$resource',
function($resource){
return $resource('/post/:PostId', {}, {
//query: { isArray:true }
});
}]);
然后在我的控制器视图中我有
MyControllers.controller('PostEditCtrl', ['$scope', '$routeParams', 'Post', '$location', 'flash', '$http', '$rootScope', function($scope, $routeParams, Post, $location, flash, $http, $rootScope ) {
$scope.post = Post.get({PostId: $routeParams.PostId});
当我保存帖子时,如果不存在,我想添加一个字段($ scope.post.owner)来在我的模型中添加关系,所以我使用这个函数:
$scope.savePost = function () {
if ($rootScope.currentUser) {
$scope.post.owner = $rootScope.currentUser;
}
$scope.post.$save({ PostId: $scope.post.id });
$location.path("/post/"+$routeParams.PostId);
};
我没有错误但是,我没有在帖子请求中看到新字段。 到目前为止,我还尝试使用$ scope.post.push(),但没有运气。