I am new to AngularJS and I am trying to put together a practice WebApp, In the earlier versions of My App I had a single Controller and I used the $http Service to fetch Data from a JSON file like so
var App = angular.module('testApp', []);
App.controller('firstCtrl', ['$scope', '$http', function($scope, $http) {
$scope.items = [];
$http.get('data.json').success(function(data) {
$scope.items = data;
});
}]);
The $http Service worked fine, Then as I progressed I had Multiple Controllers and to Share the Data between the Controllers I moved the $http Service to a Factory like so
var App = angular.module('testApp', []);
App.factory('DataFactory', ['$http', function($http) {
var DataFactory = {};
var items = [];
$http.get('data.json').success(function(data) {
items = data;
});
DataFactory.data = function() {
return items;
};
return DataFactory;
}]);
App.controller('firstCtrl', ['$scope', 'DataFactory',function($scope, DataFactory) {
$scope.items = [];
$scope.items = DataFactory.data();
}]);
And here is the HTML
<div ng-controller="firstCtrl">
<ul>
<li ng-repeat="item in items">{{item.name}}</li>
</ul>
</div>
I don't know why for some reason the Data is not being Displayed after I moved the $http Service to a Factory, What am I doing wrong, I don't see any errors in the Console