角度服务依赖注入不起作用

时间:2014-07-19 23:03:29

标签: angularjs service factory

我无法让Angular.js依赖注入自定义服务。

这是我的自定义工厂,它确实从自定义API中接收数据。

myApp.factory('myDataService', ['$http', function($http){

   myDataService.getData = function() {

    $http.get('/api/reviews')
    .success(function(data, status, headers, config) {

        console.log("Data received");
        return data;

    })
    .error(function(data, status, headers, config) {
        console.error("No data received");
    })

};

return myDataService;

}]);

但是当我在主控制器中注入服务时,编译器声明myDataService未定义。

var myApp = angular.module('myApp', []);

myApp.controller('ListCtrl', ['$scope', 'myDataService', function($scope, myDataService) {


    $scope.books = myDataService.getData();

...

为什么?

0 个答案:

没有答案