I'm a beginner with AngularJS and could use some assistance!
My issue involves retrieving JSON data from a URL in a factory function and returning it to the controller. However, I keep getting an error stating that the function doesn't exist. While I've been following a tutorial series on Udemy, the source of the data I'm working with is different, so the solution isn't covered in the videos.
Code:
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:
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="">
Thanks for any help you can provide, Matt!