在角度(离子)

时间:2017-01-07 19:18:35

标签: angularjs ionic-framework wordpress-rest-api

我正在尝试使用离子(角度)应用中的wp-rest-api v2从wordpress网站加载每个帖子,然后将此列表中的每个帖子链接到所需的帖子和页面,问题是帖子ID未显示,所以,如果我将鼠标悬停在posts.html中的任何帖子上,我只会看到#/ app / posts /的链接,而不是例如#/ app / posts / 4821(这是示例帖子的ID)

// in App.js I have the route for this pages 

  .state('app.posts', {
    url: '/posts',
    data : { auth : true },
    cache : false,
    views: {
      'menuContent': {
        templateUrl: 'templates/posts.html',
        controller : 'PostsCtrl'
      }
    }
  })
  .state('app.postDetails', {
    url: "/postDetail/:postId",
    views: {
      'menuContent': {
        templateUrl: 'templates/postDetail.html',
        controller : 'postDetailCtrl'
      }
    }
  })


  //in controller.js I have the PostsCtrl 
  .controller('postDetailCtrl', function($scope, $http, $stateParams, $sce) {
    $http.get('http://example.com/wp-json/wp/v2/posts/' + $stateParams.postId).then(
      function(returnedData){
        $scope.postDetails = returnedData.data;
        console.log($scope.postDetails);
        $scope.post_title = $sce.trustAsHtml($scope.postDetails.title.rendered);
        $scope.post_content = $sce.trustAsHtml($scope.postDetails.content.rendered);

      }, function(err){
        console.log(err);
      })

})
<!--This will load all the posts in posts.html template -->
<ion-item class="item item-avatar item-text-wrap" ng-repeat="recentPost in recentPosts | filter: searchText" href="#/app/posts/{{post.ID}}">      
 </ion-item>



<!-- this is the postDetails.html, template for each post--> 
   <div class="item item-avatar">
     <div class="text-right item-text-wrap" ng-bind-html="post_title"></div>
      </div>

     <div class="item item-image">
        <img ng-src="{{post_image}}">
     </div>

     <div class="item" dir="rtl">
       <p class="text-right item-text-wrap" ng-bind-html="post_content"></p>
     </div>

1 个答案:

答案 0 :(得分:1)

您没有正确访问帖子项目,而是使用post.ID而不是recentPost.ID。我认为其他一切都很好。

它应该是这样的:

<ion-item class="item item-avatar item-text-wrap" ng-repeat="recentPost in recentPosts | filter: searchText" href="#/app/posts/{{recentPost.ID}}">      

甚至比href更好的是使用ui-sref来声明状态和参数,并让angular构建url。像这样:

<ion-item class="item item-avatar item-text-wrap" ng-repeat="recentPost in recentPosts | filter: searchText" ui-sref="app.postDetails({postId: recentPost.ID})">