I have a local object which I am trying to turn into a factory so it can be used by my main controller:
//factory
app.factory('items', function($http) {
var items= {}; // define factory object
var items = $http.get('pathtojson/data.json').success(function(response) {
return response.data;
});
return items; // returning factory to make it ready to be pulled by the controller
});
Inside the Controller (Mainctrl):
<div class="container" ng-repeat="item in items.data" ...
However, nothing happens. I tested it first by pasting the json as an array directly into the factory so I had:
app.factory("items", function () {
var items = {};
items.data = [{name: "value"}, {name: "value1"}etc. }];
return items;
});
The above worked fine. I am using this thread as a reference but cannot get it to work. What am I doing wrong?
================
EDIT
var app = angular.module('app', []);
//factory
app.factory('itemsFactory', function($http) {
return {
getItems: function () {
return $http.get('pathtojson/data.json');
}
}
});
//controller
app.controller('MainCtrl', function($scope, itemsFactory){
itemsFactory.getItems().then(function(response){
$scope.items = response;
});
});