If I understood this correctly, you want to load a list of posts in your posts.html (posts state) and after one of those posts is clicked you want to redirect to read.html (reads state). You could do the following:
var App = angular.module("angularApp",["ui.router"]);
App.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('posts',{
url : '/posts',
templateUrl : 'views/posts.html',
controller: 'mainController as vm'
resolve: {
posts: ['$http','$q',function($http, $q){
//This call should be transfered to a service (create a new factory that will handle all of your communication to backend services)
var deferred = $q.defer();
$http.post('db.php').then(function(response){
deferred.resolve(response.data);
}).catch(function(err){
deferred.reject();
});
return deferred.promise;
}]
}
})
.state('read',{
url : 'post/:postid',
templateUrl : 'views/read.html',
controller: 'postController as vm',
resolve: {
post: ['http','$q','$stateParams', function($http, $q, $stateParams){
//Make a new call that will get your post by the post id provided in the state params ( **this is the post selected on posts state** ) - replace the test.php with a proper query that will get you the wanted post
var deferred = $q.defer();
//Something like the following:
$http.post('test.php', $stateParams.postid).then(function(response){
deferred.resolve(response.data);
}).catch(function(err){
deferred.reject();
});
return deferred.promise;
}]
}
})
});
And then in mainController ( I would name this controller postsController) you just inject the posts that you have previously acquired on state resolve
App.controller("mainController",function($scope,$http, posts){
var vm = this;
vm.posts = posts;
});
And in your posts.html view:
<div class = 'posts'>
<ul>
<li ng-repeat="post in vm.posts"><a ui-sref="read({postid: post.id})">{{post.title}}</a></li>
</ul>
With this approach you can remove the following line to avoid double initialization if you have it in your view:
<div ng-controller="mainController"></div>
Then you can create a new controller that will be executed on read state:
App.controller("postController",function($scope,$http, post){
var vm = this;
vm.post = post;
});
And in your read.html view:
<div class = 'readpage'>
<div>{{vm.post.id}}</div>
<div>{{vm.post.title}}</div>
<div>{{vm.post.body}}</div>
<div>{{vm.post.author}}</div>
</div>
Hope this helps.
If you are not familiar with the "controllerAs" syntax please check out the following: Angular 1 Style Guide