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.

bltzrrr
  • 789
  • 1
  • 8
  • 14
  • 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. – blamb Oct 21 '15 at 09:18
  • Yes, from the hard drive. I just need it for testing purposes. – bltzrrr Oct 21 '15 at 09:19
  • edited comment above.. Do you know how to load a service from your app.controller? – blamb Oct 21 '15 at 09: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. – Marcio Oct 21 '15 at 09: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. – bltzrrr Oct 21 '15 at 09:24
  • @bltzrrr http://stackoverflow.com/a/30505788/4696809 see this answer – Keval Bhatt Oct 21 '15 at 09:35
  • @bltzrrr — "From the hard drive" - do you mean "from your visitor's hard drive" or "from my site, I'm just trying to do web development without a web server"? – Quentin Oct 21 '15 at 09:43
  • @Quentin From my own project folder. Maybe I should've mentioned I use XAMPP and have that file in my project directory. – bltzrrr Oct 21 '15 at 09:47
  • @bltzrrr — So its a URL on the same origin then? (That fact the server gets the data from the hard disk is mostly irrelevant). – Quentin Oct 21 '15 at 09:49

2 Answers2

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);
      })

})
Shushanth Pallegar
  • 2,832
  • 1
  • 14
  • 16
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.

Community
  • 1
  • 1
blamb
  • 4,220
  • 4
  • 32
  • 50