I have a scenario where I need to call a factory from multiple controllers in order to fetch JSON data. When I tried calling the factory directly from the controller, it worked successfully. However, after adding a factory and checking the console for errors (which is empty), nothing appears on the screen either. Here is the code snippet that illustrates the issue:
HTML template:
<!-- Schools -->
<div class="row">
<h3 class="text-center">Schools and Associations</h3>
<ul class="gallery gallery1" ng-controller="SchoolsCtrl">
<li ng-repeat="school in schools" class="card">
<a ng-href="{{school.url}}" target="_blank">
<img ng-src="{{school.thumbUrl}}" class="img-responsive">
<p class="text-center">{{school.name}}</p>
<p class="text-center">{{school.time}}</p>
</a>
</li>
</ul>
</div>
controller.js:
var mainApp = angular.module('mainApp', ['bootstrapLightbox', 'ngAnimate']);
mainApp.controller('CompaniesCtrl', function($scope, GalleryService) {
$scope.companies = GalleryService.getObjects('app/common/companies.json');
});
mainApp.controller('WorksCtrl', function($scope, GalleryService) {
$scope.works = GalleryService.getObjects('app/common/freelance-projects.json');
});
mainApp.controller('SchoolsCtrl', function ($scope, GalleryService) {
$scope.schools = GalleryService.getObjects("app/common/schools.json");
});
service.js:
mainApp.factory('GalleryService', function ($http) {
return {
getObjects: function (jsonUrl) {
$http.get(jsonUrl)
.success(function (data) {
return data;
})
.error(function (data, status, error, config) {
return [{heading:"Error", description: "Could not load json data"}];
});
}
}
});