Update Json File in a static Angularjs Website -
i'm trying make static blog in website. have json file contains posts displayed. have modal window create new post. can see result , site shows new post, when page reloaded new post gone. i'd update json file without using backend, using angularjs, possible?
i tried without success:
posts.html:
<div class="row" data-ng-repeat="post in posts"> <div class="col-md-1 text-center"> <p>june 17, 2014</p> </div> <div class="col-md-5"> <a ui-sref="/posts/{{ posts.indexof(post) }}"> <img class="img-responsive img-hover" src="http://placehold.it/600x300" alt=""> </a> </div> <div class="col-md-6"> <h3> <a href="#/post/{{ posts.indexof(post) }}">{{ post.title }}</a> </h3> <p>by <a href="#/post/{{ posts.indexof(post) }}">{{ post.author }}</a> </p> <p>{{ post.exerpt }}</p> <a class="btn btn-primary" href="#/post/{{ posts.indexof(post) }}">read more <i class="fa fa-angle-right"></i></a> </div> <br/> <hr> </div> <button class="btn btn-success" ng-click="showmodal()">add new post</button>
posts.js:
.controller('postsctrl', ['$scope', '$http', '$modal', function($scope, $http, $modal){ $http.get('data/posts.json').success(function(data){ $scope.posts = data; }); $scope.showmodal=function () { $scope.newpost={}; var modalinstance = $modal.open({ url:'/addpost', templateurl: 'views/addpost.html', controller:'addpostctrl', resolve: { newpost: function() { return $scope.newpost; } } }); modalinstance.result.then(function(selecteditem) { $scope.posts.push({ author: $scope.newpost.author, title: $scope.newpost.title, exerpt: $scope.newpost.exerpt, content: $scope.newpost.content }); }); }; }]);
addpost.html:
<div class="modal-header"> <button type="button" class="close" ng-click="cancel()" data-dismiss="modal" aria-hidden="true">×</button> <h1>new post</h1> </div> <div class="modal-body"> <label>author</label><input type="text" ng-model="newpost.author"/> <label>title</label><input type="text" ng-model="newpost.title"/> <label>summary</label><textarea type="text" ng-model="newpost.exerpt"/> <label>content</label><textarea type="text" ng-model="newpost.content"/> <br/> <button class="btn btn-success" ng-click="addnewpost()"> add post</button> </div>
addpost.js:
.controller('addpostctrl',['$scope', '$modalinstance','newpost','$http', function ($scope, $modalinstance,newpost,$http) { $scope.newpost=newpost; $scope.addnewpost=function(newpost){ $http.get('data/posts.json').success(function(data){ $scope.posts = data; $scope.posts.push({newpost: newpost}); }); $modalinstance.close(newpost); }; $scope.cancel =function(){ $modalinstance.dismiss('cancel'); }; }]);
i don't know if use angular local storage or other solution.
thanks lot help!!!!
Comments
Post a Comment