2

Is there a way to make a get request using a factory service, in order to get JSON data locally? I have one big file that I need to get from a factory in order to do some testing.

9
  • 1
    Of course, but what do you mean by locally? from hard drive to browser client, or what? I want to say you make your separate service to make the calls, load in that service module, then you call those functions from your scope. Is that partially what your asking? I can fill you in with a bit more detail if so. Commented Oct 21, 2015 at 9:18
  • Yes, from the hard drive. I just need it for testing purposes. Commented Oct 21, 2015 at 9:19
  • edited comment above.. Do you know how to load a service from your app.controller? Commented Oct 21, 2015 at 9:21
  • put it in the public folder and read the file. Another way if you don't want to move the file is to use nodejs. The client side would ask nodejs to get the file. Nodejs would read the file and answer it to the client. Commented Oct 21, 2015 at 9:24
  • @Brian Thomas Exactly that. The service shouldn't be complicated. I just want to pass the JSON data to an array I have in my controller. I believe it's quite simple, but have never managed to do it locally for some reason, there's always something that pops up. Commented Oct 21, 2015 at 9:24

2 Answers 2

3

use $http service to get the data from your local.

factory

 App.factory('getJSon',function('$http'){ return{ getjson : function(localJSONPath){ return $http.get(localJSONPath) // path of the json file } } }); 

controller

App.controller('myCtrl',function(getJSon){ //uses service $scope.jsonPath = 'files/dataFile.json' $scope.jsonFile = getJson.getjson($scope.jsonPath); //returns promise jsonFile.then(function(data){ //do things here console.log(data); }) }) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thats pretty slick, but are the files local to the server that way? If so, that would work great. Throw that as a service, pass the path param through scope, though.
@BrianThomas edited the answer as u requested
That was of course my first solution, but once it started returning 'Unexpected token d', I gave it up.
@bltzrrr is your JSON valid JSON ? ., just validate it over jsonlint.com
1

If you dont have node involved you can use strictly Angular for this, using a directive, something like this might be helpful. How to read a file in AngularJS?

Otherwise you can load the file using nodes fs module, load that on your service, so delegate this code to a new service file. ON this service put your logic to do the dirty work, call the readFile there. Load the service module into your app. Call those functions through your scope. e.g. on the readFile nodejs load file

And you dont really need to make a service for this, you can load it all on your app.js file if you need to, if its temp or something.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.