AngularJS无法读取VS中未定义的属性'getData'

时间:2015-10-12 12:19:48

标签: json angularjs

你知道我做错了吗?我想从我的json文件中读取数据,但是我得到了它无法读取属性getData的错误。

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

控制器:

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

我在mvc项目中使用Visual Studio中的ng。

路径json-file:“Visual Studio 2015 \ Projects \ Test \ WebApplication \ Scripts \ jsonData \ Stations.json”

1 个答案:

答案 0 :(得分:1)

在您共享的控制器代码中,您尚未正确注入“jsonDataService”服务。

应该是:

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