0

do you know what I am doing wrong? I want to read data from my json-file but i got the error that it can´t read the property getData.

myApp.service('jsonDataService', function ($http) {
this.getData = function () {
    return $http({
        method: 'GET',
        url: '/jsonData/Stations.json'
    });
  }
});

controller:

myApp.controller('IndexController', ['$scope', function ($scope, jsonDataService) { 
jsonDataService.getData().then(function (msg) {
    $scope.msg = msg;
    console.log(msg);
   });
}]);

I am using ng in Visual studio in a mvc project.

path json-file: " Visual Studio 2015\Projects\Test\WebApplication\Scripts\jsonData\Stations.json"

trap
  • 2,550
  • 7
  • 21
  • 42

1 Answers1

1

In the controller code which you have shared, you have not injected 'jsonDataService' service properly.

It should be:

myApp.controller('IndexController', ['$scope', 'jsonDataService', function ($scope, jsonDataService) { 
    jsonDataService.getData().then(function (msg) {
    $scope.msg = msg;
    console.log(msg);
   });
}]);
Prabhas Nayak
  • 282
  • 1
  • 4