Trying to figure out how to work with frontend and a JSON file for my data, without the usual node/express backend setup. Currently, I have a list of posts displaying but now I want to be able to fetch the unique post:id when clicking on a button.
A simple example of posts.json:
[{
id: 1,
title: 'Simple title1',
content: 'Sample content...',
permalink: 'simple-title1',
author: 'Peter',
datePublished: '2012-04-04'
}, {
id: 2,
title: 'Simple title2',
content: 'Sample content...',
permalink: 'simple-title2',
author: 'Peter',
datePublished: '2012-05-04'
}, {
id: 3,
title: 'Simple title3',
content: 'Sample content...',
permalink: 'simple-title3',
author: 'Thomas',
datePublished: '2012-06-04'
}]
Snippet from my app.js file:
var routerApp = angular.module('routerApp', ['ui.router']);
routerApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/posts',
templateUrl: 'partial-home.html',
controller: function($scope, $http){
$http.get('posts.json')
.success(function(data){
$scope.posts = data;
});
}
})
.state('viewdetails',{
url:'/posts/:id/:permalink',
templateUrl: 'view_details.html',
controller: function($scope, $http){
$http.get('source.json')
//get a single post here???
} }
Coding snippet from partial-home.html:
<div class="row" style="margin-left:50px">
<div class="col-xs-8 col-sm-4 col-md-3 col-lg-5 box-background" ng-repeat="post in posts">
<div class="col-md-4 img-space">
<img class="img-circle" alt="Bootstrap Image Preview" src="{{prov.picture}}" /></div>
<div class="col-md-4">
<h4>{{post.author}}</h4>
<p class="text-grey">
{{post.content}}
</p>
</div>
<a ui-sref="posts/:id" class="btn btn-primary btn-color" style="width:100%">View details</a>
</div>
</div>
Looking for a simple solution without using a traditional MVC pattern as I'm used to working with a mongoose and node backend. Any suggestions or ideas are greatly appreciated. Thank you!