Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  152] [ 2]  / answers: 1 / hits: 29763  / 10 Years ago, sun, april 20, 2014, 12:00:00

I'm very new to both web development and AngularJS. I am trying to write a web page that will automatically update its info based on JSon files sitting on my server. I can get the json data but I can't seem to parse the data coming in. I validated the json data just to make sure I was writing it correctly, but whenever I display it on the site is just displays as a single string. I am not able to access the individual members. My factory and controller are below. Any help would be greatly appreciated!!



var MyController = function($scope, $log, MyFactory) {
$scope.notes =[];

function init() {
MyFactory.getNotes()
.success(function(notes){
$scope.notes = JSON.parse(notes);
})
.error(function(data, status, headers, config) {
$log.log(data.error + ' ' + status);
});
}

init();

angular.module('MyApp')
.controller('MyController', MyController);
};


And the factory:



    var MyFactory = function($http) {
var factory = {};
factory.getNotes = function() {
return $http.get('/ci/data.json');
};
return factory;
};

angular.module('MyApp').factory('MyFactory',
MyFactory);


I admit the code and question is crude but I've just started. Any additional help on architecture and style would be appreciated as well! Thanks in advance!


More From » json

 Answers
8

  1. You don't need to parse the response as json. If the sourcefile is giving you valid json, angular knows its json.


  2. In your controller, try to console.log($scope.notes). If using google chrome, you get the json results pretty printed in the console you know your factory is working.


  3. Use an angular forEach to iterate through the result to do something with it.



    angular.forEach($scope.notes, function(note) {
    console.log(note);
    });


[#71381] Thursday, April 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neilshamarh

Total Points: 181
Total Questions: 94
Total Answers: 104

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;