read a .json file in angularjs

1

Hi I need to read a json file in angular using a service, using the http service

app.factory('Expenses', function($http){
var service = {};

service.entries = [];

$http.get('data/get_all.json').then(function (data){
        service.entries = data;

        //convertir fecha de string a objeto
        service.entries.forEach(function(element){
            element.date = myHelpers.stringToDateObj(element.date);
        });
    }).catch(function(data, status){
    alert('error no se puede leer el archivo!');
    });

I'm using angular 1.6.7 and change the file angular.min.js --- > angular 1.5.9 to make tests.

Help, do not read the file, send me the alert.

had success and change it by then, change error by catch

    
asked by Estefania 13.02.2018 в 17:05
source

1 answer

0

If you look at the code that the browser receives, you will notice that the changes you mentioned have not been reflected (success by then). The error that arises is that the success function is not defined. Surely this happens because the browser has the app.js code in cache. What you can do to avoid that is, in the src attribute of the script tag add a parameter to the url, for example

 src="./app.js?x=7"

If you are editing app.js you could frequently generate a random value for the url parameter, so the browser will not realize that it is the same url and will load the modified file each time you reload the page

    
answered by 13.02.2018 в 18:03