检索帖子ID时出错

时间:2015-05-10 16:03:48

标签: javascript angularjs

从WordPress帐户我显示帖子的标题,如果您点击任何标题,您将被重定向到新视图以检查整个帖子。

这里是html

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

并在此处查看完整帖子的视图

<h3>{{:: post.content}}</h3>

这就是我在那个观点中所拥有的一切。

查看代码

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

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

/news是您可以看到标题的主视图,'/news/:postId是您可以看到整个帖子的视图

.controller('NewsCtrl', function($scope, FreshlyPressed) {
  $scope.posts = [];
  $scope.doRefresh = function() {
    $scope.posts = FreshlyPressed.getBlogs($scope);
  }
});

服务

.service('FreshlyPressed', function($http) {
  return {
    getBlogs: function($scope) {
      $scope.posts = [];
      $http.jsonp('https://public-api.wordpress.com/rest/v1.1/freshly-pressed?callback=JSON_CALLBACK')
        .success(function(result) {
          $scope.posts = result.posts;
        });
    },

    get: function(postId, $scope) {
      console.log(postId);
      console.log($scope.posts);
      for (var i = 0; i < $scope.posts.length; i++) {
        if ($scope.posts[i].id === parseInt(postId)) {
          return $scope.posts[i];
        }
      }
      return null;
    }
  }
})

/news/:postId的控制器

.controller('PostDetailCtrl', function($scope, $stateParams, FreshlyPressed) {
  $scope.post = FreshlyPressed.get($stateParams.postId, $scope);
});

我到目前为止已将posts视为未定义且TypeError: Cannot read property 'length' of undefined

我做错了什么?

这是我从结果对象

获得的内容
{
    date_range: {
        newest: "2015-05-09T20:02:01+00:00",
        oldest: "2015-05-06T20:02:02+00:00"
    },
    number: 10,
    posts: [{
        ID: 706,
        site_ID: 42659653,
        author: {
            ID: 42088694,
            login: "oliviaacole",
            email: false,
            name: "oliviaacole",
            nice_name: "oliviaacole",
            URL: "http://oliviaacole.wordpress.com",
            avatar_URL: "https://1.gravatar.com/avatar/adea7829a98ebdba180a6ff6ce1beeef?s=96&d=identicon&r=PG",
            profile_URL: "http://en.gravatar.com/oliviaacole",
            site_ID: 42659653
        },
        date: "2014-12-03T18:07:51-06:00",
        modified: "2015-04-30T08:14:23-05:00",
        title: "White Rage, the Hunger Games, and the Lack of Justice for Eric Garner",
        URL: "https://oliviaacole.wordpress.com/2014/12/03/white-rage-the-hunger-games-and-the-lack-of-justice-for-eric-garner/",
        short_URL: "http://wp.me/p2SZIV-bo",
        content: "<p><a href="
        https: //oliviaacole.files.wordpress.com/2014/12/peacekeeper.jpg"><img class="aligncenter wp-image-707" src="https://oliviaacole.files.wordpress.com/2014/12/peacekeeper.jpg?w=494&#038;h=214" alt="eric garner" width="494" height="214" /></a></p> <p>Today, like too many days, I am angry. Today a <a </p> ",
        excerpt: "<p>Today, like too many days, I am angry. Today a grand jury voted not to bring criminal charges against the white officer who killed Eric Garner, father of six, with a chokehold. The killing is on video, which many people hoped would mean an indictment and, eventually, a conviction. Not so. Today, America tells us [&hellip;]</p> ",
        slug: "white-rage-the-hunger-games-and-the-lack-of-justice-for-eric-garner",
        guid: "http://oliviaacole.wordpress.com/?p=706",
        status: "publish",
        sticky: false,
        password: "",
        parent: false,
        type: "post",
        comments_open: true,
        pings_open: true,
        likes_enabled: true,
        sharing_enabled: true,
        comment_count: 40,
        like_count: 74,
        i_like: 0,
        is_reblogged: 0,
        is_following: 0,
        global_ID: "7235f7d1c1e185bcc0fbd19cb0b3b088",
        featured_image: "",
        post_thumbnail: null,
        format: "standard",
        geo: false,
        menu_order: 0,
        publicize_URLs: [],
        tags: {

1 个答案:

答案 0 :(得分:0)

不要将范围传递给服务,而是从服务中返回$http承诺

修改后的服务方法

getBlogs: function() {

 return $http.jsonp('https://public....essed?callback=JSON_CALLBACK')
         .error(function(){ alert('oops...error');});
 }

控制器

 FreshlyPressed.getBlogs().success(function(result){
      console.log(result);//have a peak at response in console
      $scope.posts = result.posts;
 });