You could also resolve the data in the route:
angular.module('app.something')
.config(routing);
routing.$inject = ['$stateProvider'];
function routing(stateProvider: angular.ui.IStateProvider) {
stateProvider
.state('home', {
url: '/home',
templateUrl: 'scripts/home/home.html',
controller: 'app.something.BaHomeController',
controllerAs: 'vm',
resolve: {
entries: loadEntries
}
})
}
function loadEntries() {
//Make call to entries service
return entriesService.getEntries(); //Make the http call in the service and also deal with the promise there.
}
Your controller
class BaHomeController{
static $inject = ["$http"];
constructor(private $http: ng.IHttpService,
public entries: any){ }
}
An alternative is to have an init function in the controller and call it from the html so it gets invoked when the page loads
class LayoutController {
dataModel: app.layout.TopSearchHtmlDataModel;
vehicleSearchModel: app.layout.VehicleSearchModel;
static $inject = [
'app.services.SlideService',
'app.services.VehicleService',
'app.services.CommonUtils',
'$rootScope',
'$filter',
'$state'
];
constructor(
private slideService: app.services.SlideService,
private vehicleService: app.services.VehicleService,
private commonUtils: app.services.CommonUtils,
private $rootScope: ng.IRootScopeService,
private $filter: any,
public $state: ng.ui.IStateService) {
this.dataModel = new app.layout.TopSearchHtmlDataModel();
this.vehicleSearchModel = new app.layout.VehicleSearchModel();
}
init(): void {
this.getAllMakeAndModelData();
this.getIrishCounties();
this.getMinYearArray();
this.getLogoSlides();
}
//Private methods omitted
}
html:
<div ng-init="vm.init()"></div> //assuming you use controllerAs vm