Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 如何访问参数以获取$resource中的单个对象?_Angularjs_Angularjs Ng Resource - Fatal编程技术网

Angularjs 如何访问参数以获取$resource中的单个对象?

Angularjs 如何访问参数以获取$resource中的单个对象?,angularjs,angularjs-ng-resource,Angularjs,Angularjs Ng Resource,因此,我试图从帖子列表中获取一个“post”对象,但我不知道传递的params对象名的名称。 这里是PostDetails组件 "严格使用", angular.module('postDetail', ['ngRoute', 'postsService']) .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/posts/:postId', { templateUrl: 'post-

因此,我试图从帖子列表中获取一个“post”对象,但我不知道传递的params对象名的名称。 这里是PostDetails组件 "严格使用",

angular.module('postDetail', ['ngRoute', 'postsService'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/posts/:postId', {
    templateUrl: 'post-detail/post-detail.template.html',
    controller: 'PostDetailController'
  });
}])

.controller('PostDetailController', ['$scope', '$http', 'postsService', '$routeParams', function($scope, $http, postsService, $routeParams) {
    $scope.post = postsService.getPost($routeParams.postId);
}]);
在这里,我将posted传递给getPost方法。顺便说一句,这里postId的值是有效的

这是我的邮政服务

angular.
  module('postsService', ['ngResource']).
  factory('postsService', ['$resource',
    function($resource) {
      return $resource('https://jsonplaceholder.typicode.com/posts/:postId', {}, {
        getPost: {
          method: 'GET',
          params: {postId: '@postId'}
        }
      });
    }
  ]);
但是这里的“@postId”表达式是空的,它试图获取帖子列表。如何引用传递的参数

我的帖子详细信息模板

<h3>{{post.title}}</h3>
<p>{{post.body}}</p>
{{post.title}
{{post.body}

而不是:

$scope.post=postsService.getPost($routeParams.postId)

做:

查看中如何使用
$resource
类方法的示例

$scope.post = postsService.getPost({
    postId: $routeParams.postId
});