gpt4 book ai didi

javascript - $resource.get 给出 "TypeError: undefined is not a function"

转载 作者:行者123 更新时间:2023-11-29 21:52:26 25 4
gpt4 key购买 nike

这个简单的 Angular 应用有什么问题:

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

angular.module('app', [

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

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

}]);
}());

以及 Controller 和资源:

// 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"}... line:

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)

我已经检查了与此相关的其他问题,但我找不到问题。

最佳答案

依赖注入(inject)不正确,你应该注入(inject)Entry服务,而不是$resource:

Weather.$inject = ['Entry'];

关于javascript - $resource.get 给出 "TypeError: undefined is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com