In my AngularJS 1.5 app I have some controller data that directly links to the service data. When the user hits the page, the controller calls init in the service and then a data service is called to retrieve the data from the server.
E.g.
//inside controller
$scope.data = ClockingMenuService.data;
ClockingMenuService.init();
//inside service
function ClockingMenuService() {
var dataObj = {};
var defaultData = {
startWeek: null,
endWeek: null,
statusCode: 0
};
Object.assign(dataObj, defaultData);
return {
data: dataObj,
};
function init() {
ClockingDataService
.getClockingDataFromServer(dataObj.startWeek, dataObj.endWeek)
.then(function(response) { dataObj = response; })
.catch(handleError);
}
}
I am working on moving the app to Angular 5 and I would like the similar functionality.
How could this type of thing be implemented in Angular 5? Is it best practice to put the data in the component or in the service?