I'm struggling to understand why my controller is receiving an empty array after calling the service, which triggers an http call. Here's how I have set up my app:
Here is my app.js file:
//App.js
.state('myApp.sermonlists', {
url: "/sermonlists",
views: {
'menuContent': {
templateUrl: "templates/sermonlists.html",
controller: 'SermonListsCtrl'
}
}
})
This is controller.js:
//controller.js
angular.module('myApp.controllers', [])
.controller('SermonListsCtrl', function($scope, sermonListsService) {
// call sermon api
sermonListsService.getSermonLists(); // this returns an empty array()
})
This is services.js:
//services.js
angular.module('myApp.services', [])
.factory('sermonListsService', function($http) {
var sermon = [];
function sermonJson() {
$http.get('http://myapicall.com').then(function(resp) {
// For JSON responses, resp.data contains the result
sermon = resp.data;
}, function(err) {
console.error('ERR', err);
// err.status will contain the status code
})
}
// initializing sermonJson call
function init() {
sermonJson();
}
// run init()
init();
return {
getSermonLists: function(){
return sermon;
},
getUser: function(index){
return "";
}
}
})
I would appreciate any guidance on best practices and examples to help me resolve this issue.