Reading json file via Angularjs service

Asked

Viewed 846 times

2

I am 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’s statement

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

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

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

error :

Uncaught Syntaxerror: Unexpected token (

2 answers

1


I discovered the mistake;;

I was declaring a Factory as a service, it shall have the following structure


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

    var factory = {}; 

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

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

    return factory;

});

  • Mark your answer as "Accept" to signal that this answer solved your problem. = D

1

  • While this link may answer the question, it is best to include the essential parts of the answer here , and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.