Struggling to populate data from a service into my view. Here is how I have defined the service:
app.factory('nukeService', function($rootScope, $http) {
var nukeService = {};
nukeService.nuke = {};
//Retrieves list of nuclear weapons
nukeService.getNukes = function() {
$http.get('nukes/nukes.json')
.success(function(data) {
nukeService.nukes = data;
});
return nukeService.nukes;
};
return nukeService;
});
And this is my controller:
function NavigationCtrl($scope, $http, nukeService){
/*$http.get('nukes/nukes.json').success(function(data) {
$scope.nukes = data;
});*/
$scope.nukes = nukeService.getNukes();
}
When accessing the data directly from the controller using $http.get it works fine. However, when attempting to retrieve it from the service, nothing shows up. I am aware that the query is asynchronous but struggling to figure out how to update the $scope variable once the data is returned. Utilizing $rootscope to broadcast an event and listen for it in the controller seems like a workaround rather than a proper solution. Any guidance on the correct approach would be greatly appreciated.