$ resource.get给出" TypeError:undefined不是函数"

时间:2015-02-07 20:33:28

标签: javascript angularjs

这个简单的Angular应用程序出了什么问题:

// app.js
(function () {
    'use strict';

    angular.module('app', [

        'ngRoute',
        'ngResource',
        'app.weather'

    ])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider.otherwise({redirectTo: '/'});

    }]);
}());

控制器和资源:

// weather.js
(function () {
    'use strict';

    // Resource
    angular.module('app.weather',['ngResource'])
        .factory('Entry', function($resource) {
            return $resource('http://api.openweathermap.org/data/2.5/weather');
        });

    // Controller
    angular
        .module('app.weather')
        .controller('Weather', Weather);

    Weather.$inject = ['$resource'];

    function Weather(Entry) {
        var vm = this;
        var result = Entry.get({q: "London,uk"}, function() {
            vm.data = result;
            console.log(result);
        });
    }

}());

它提供以下错误,该错误引用Entry.get({q: "London,uk"}...行:

TypeError: undefined is not a function
    at new Weather (weather.js:25)
    at Object.invoke (angular.js:4185)
    at $get.extend.instance (angular.js:8454)
    at angular.js:7700
    at forEach (angular.js:331)
    at nodeLinkFn (angular.js:7699)
    at compositeLinkFn (angular.js:7078)
    at compositeLinkFn (angular.js:7081)
    at compositeLinkFn (angular.js:7081)
    at publicLinkFn (angular.js:6957)

我已经检查了其他有关此问题,但我无法解决问题。

1 个答案:

答案 0 :(得分:2)

依赖注入不正确,您应该注入Entry服务,而不是$resource

Weather.$inject = ['Entry'];