I am trying to load each posts from wordpress site using wp-rest-api v2 in ionic(angular) app and then link each post in this list to desired post and page, the problem is the post id are not shown, so if I hover on any of the posts in posts.html I just see link to #/app/posts/ instead of for example #/app/posts/4821(it is the id of sample post)
// 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>
You are not accesing the post item correctly, you are using post.ID instead of recentPost.ID. I think everything else is allright.
It should look like this:
Even better than href is using ui-sref to declare the state and the parameters and let angular build the url. Like this: