按ID检索帖子

时间:2015-05-12 06:20:03

标签: javascript json angularjs wordpress http

我从WordPress收到一个JSON对象,看起来像这样

{
        "ID": 4164,
        "title": "24 Horas Non-Stop con Marco Carola",
        "status": "publish",
        "type": "post",
        "author": {
            "ID": 11,
            "username": "VIlma Quiros",
            "registered": "2015-04-16T07:04:04+00:00",
            "meta": {
                "links": {
                    "self": "http://urbanetradio.com/wp-json/users/11",
                    "archives": "http://urbanetradio.com/wp-json/users/11/posts"
                }
            }
        },
        "content": "<p class=\"p2\"><a href= 

这是我的服务

.service('FreshlyPressed', function($http, $q) {
  return {

    getBlogs: function($scope) {
      var posts = [];
      $http.get('http://urbanetradio.com/wp-json/posts')
        .success(function(result) {
          $scope.posts = result;
        })
    },

    getPostById: function(postId) {
      var url ='http://urbanetradio.com/wp-json/posts/postId';
      return $http.get(url);
    }

});

这里是控制器

.controller('NewsCtrl', function($scope, FreshlyPressed) {

  $scope.posts = [];

  $scope.doRefresh = function() {
    $scope.posts = FreshlyPressed.getBlogs($scope);
    $scope.$broadcast('scroll.refreshComplete');
  }
  $scope.doRefresh();

});

这就是我想要的:

在此视图中,我只显示帖子的标题和日期,这是主视图

  <a ng-href="#/tabs/news/{{post.ID}}">
    <h2 ng-bind-html="post.title"></h2>
    <p>{{:: post.date | date}}</p>
  </a>

当您点击该标题时,您应该被重定向到次要视图中的已退休帖子

  <div class="item item-text-wrap item-image padding">
    <div class="special-font" ng-bind-html="post.content"></div>
  </div>

路线

//the route for the main view

.state('tabs.news', {
    url: '/news',
    views: {
      'tab-news': {
        templateUrl: 'templates/tab-news.html',
        controller: 'NewsCtrl'
      }
    }
  })

//the route for the second view where you will see the entire post

.state('tabs.post-detail', {
  url: '/news/:postId',
  views: {
    'tab-news': {
      templateUrl: 'templates/tab-post-detail.html',
      controller: 'PostDetailCtrl'
    }
  }
})

我收到此错误

GET http://urbanetradio.com/wp-json/posts/postId 404 (Not Found)

1 个答案:

答案 0 :(得分:2)

我想你需要像这样改变这个功能

  getPostById: function(postId) {
      var url ='http://urbanetradio.com/wp-json/posts/'+ postId;
      return $http.get(url);

根据你的代码postId是你要在字符串中替换的参数,而不是你应该在字符串中附加值,如代码

你需要调用像

这样的方法
   FreshlyPressed.getPostById(1);//1 is postid value