我想首先说我对AngularJS很新,所以任何帮助都会受到赞赏!
我正在尝试从工厂函数中的URL收集一些JSON数据,然后将数据返回给控制器,但是它会抛出一个错误,说该函数不存在。我一直在关注Udemy的教程系列,但我收集的数据来自不同的来源,所以答案不在视频中。
代码:
angular.module('MainModule', ['ngRoute']);
angular.module('MainModule').config(function($routeProvider){
$routeProvider.when('/', {
templateUrl: '/templates/index.html'
});
$routeProvider.when('/item/:id', {
templateUrl: '/templates/item.html',
});
});
angular.module('MainModule').controller('ItemsController', function($scope, ItemsFactory, MainSettings){
ItemsFactory.getItems().then(function(response){
$scope.items = response;
});
$scope.settings = MainSettings;
});
angular.module('MainModule').controller('ItemController', function($scope, $routeParams, ItemsFactory, MainSettings){
$scope.item = ItemsFactory.getItem($routeParams.id);
$scope.settings = MainSettings;
});
angular.module('MainModule').factory('ItemsFactory', function($http, $log){
var factory = {};
var items = false;
factory.getItems = function(){
$http.get('/index.php').success(function(response){
items = response.items;
return items;
});
};
factory.getItem = function(item){
var i = 0;
while (i < items.length) {
if (items[i].id == item) {
return items[i];
}
i++;
}
return false;
};
return factory;
});
angular.module('MainModule').constant('MainSettings', {
title: 'Simple RSS Reader',
version: '1.0'
});
错误:
Error: ItemsFactory.getItems(...) is undefined
@http://192.168.1.234:1000/js/angular-module.js:11:2
invoke@http://192.168.1.234:1000/js/angular.js:3965:14
instantiate@http://192.168.1.234:1000/js/angular.js:3976:23
$ControllerProvider/this.$get</<@http://192.168.1.234:1000/js/angular.js:7307:18
nodeLinkFn/<@http://192.168.1.234:1000/js/angular.js:6696:34
forEach@http://192.168.1.234:1000/js/angular.js:332:11
nodeLinkFn@http://192.168.1.234:1000/js/angular.js:6683:11
compositeLinkFn@http://192.168.1.234:1000/js/angular.js:6131:13
publicLinkFn@http://192.168.1.234:1000/js/angular.js:6027:30
ngViewFillContentFactory/<.link@http://192.168.1.234:1000/js/angular-route.js:915:7
nodeLinkFn@http://192.168.1.234:1000/js/angular.js:6737:13
compositeLinkFn@http://192.168.1.234:1000/js/angular.js:6131:13
publicLinkFn@http://192.168.1.234:1000/js/angular.js:6027:30
createBoundTranscludeFn/boundTranscludeFn@http://192.168.1.234:1000/js/angular.js:6151:21
controllersBoundTransclude@http://192.168.1.234:1000/js/angular.js:6758:18
update@http://192.168.1.234:1000/js/angular-route.js:865:25
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@http://192.168.1.234:1000/js/angular.js:13070:15
updateRoute/<@http://192.168.1.234:1000/js/angular-route.js:547:15
qFactory/defer/deferred.promise.then/wrappedCallback@http://192.168.1.234:1000/js/angular.js:11659:31
qFactory/defer/deferred.promise.then/wrappedCallback@http://192.168.1.234:1000/js/angular.js:11659:31
qFactory/ref/<.then/<@http://192.168.1.234:1000/js/angular.js:11745:26
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://192.168.1.234:1000/js/angular.js:12788:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://192.168.1.234:1000/js/angular.js:12600:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://192.168.1.234:1000/js/angular.js:12892:13
done@http://192.168.1.234:1000/js/angular.js:8427:34
completeRequest@http://192.168.1.234:1000/js/angular.js:8641:7
createHttpBackend/</xhr.onreadystatechange@http://192.168.1.234:1000/js/angular.js:8580:1
<div class="ng-scope" ng-view="">
先谢谢你,马特!
答案 0 :(得分:0)
我的问题与此类似,我通过注入我的工厂解决了这个问题 -
angular.module('MainModule').controller('ItemsController', ['ItemsFactory', function($scope, ItemsFactory, MainSettings){
ItemsFactory.getItems()
.success(function(response) {..})
.error(function(response) {..})
.then(function(response){
$scope.items = response;
});
$scope.settings = MainSettings;
}]);
如果您决定使用缩小库或RequireJS,最终会这样做。更多信息here在官方文档中。
答案 1 :(得分:0)
两个修复程序,分别和一起删除错误,但数据仍然没有返回。注入在控制器中列出的顺序是否会对它们的工作方式产生任何影响?