I am very new to Angularjs and I am having trouble figuring out how to update the $ scope element that I created from JSON. Basically, I have a service that contains a function that captures JSON:
app.service('JSONService', function($http){ return{ getJSON: function(){ return $http.get('posts.json') .then(function(response){ return response.data; }); } }; });
Then I have a controller containing a function that receives JSON data when the button is clicked and puts it in $ scope.data and a second function that I would like to use to update $ scope.data:
app.controller('PostController', function PostController($scope, JSONService){ $scope.data; $scope.getJSON = function(){ $scope.data = JSONService.getJSON(); }; $scope.addPost = function(){
Currently, I am successfully capturing JSON data and can use it to populate aspects of my view, but I'm fixated on how to continue updating $ scope.data so that:
- It actually updates
- The update is reflected in my view.
I tried $ broadcast, $ scope.data.push, $ scope.data.posts.push. They are either flat or not processed, or these errors. I am sure this may be a simple answer, but I feel that I can be inexperienced with Angularjs and JSON to pick it up. Thanks in advance.
source share