Reading json file via AngularJs service

2

I'm trying to read a Json file via a service I created:

Service:

App.factory('service', function($http) {
        var promise;

        get: function() {
            if (!promise) {
                promise = $http.get('../library/data_json.json')
                    .success(function (response) {
                        console.log('success');
                        promise.data = response;
                    });
            }
            return promise.data;
        }
    });

Controller statement

App.controller('CauController', function (service, $http, $scope) { 

Now in the Controller I call the function that will return the promise of $ http;

service.get().then(function(data) {
     $scope.dados = data;
});

error:

  

Uncaught SyntaxError: Unexpected token (

    
asked by anonymous 02.02.2016 / 14:19

2 answers

1

I discovered the error ;;

I was declaring a factory as a service, it should have the following structure


module.factory('MyService', function() {

    var factory = {}; 

    factory.method1 = function() {
            //..
        }

    factory.method2 = function() {
            //..
        }

    return factory;

});

    
02.02.2016 / 14:34
1

To add value, follow a guide to respect: link

    
18.01.2017 / 03:52