I am currently immersing myself in Angular and tackling a complex dashboard framework all built with Angular. Prior to loading the controllers, I need to fetch various dashboard settings from the server using $HTTP. These settings play a crucial role in determining the layout of the dashboards. According to my understanding, Angular builds by executing config methods first, followed by run methods, and then the controllers. Unfortunately, $HTTP cannot be used in a config method, so I have incorporated it into my main.js:
MetronicApp.run(['$rootScope','$http', function($rootScope,$http) {
var CUID = Cookies("CUID");
console.log('portlet settings for '+ CUID);
$http.get('/myurl/V3_portlet_settings?p_user_id='+CUID)
.then(function(response) {
console.log(response.data);
console.log('portlet status: ' + response.status);
$rootScope.$broadcast("dashSettings",response.data);
});
}]);
Upon running, everything functions smoothly and I can view the data in the console.
Subsequently, in my controller:
$scope.$on( "dashSettings",
function(event,data){
$scope.dData = data;
console.log('dash data service identified in dash controller');
console.log($scope.dData.count);
} );
Now, I have a couple of questions:
Is this the most effective approach for retrieving settings before initializing the dashboard? My intention is to integrate the dash-building calls within my $scope.$on block. I started exploring how to execute a run method synchronously prior to initializing the controllers, but perhaps it's unnecessary.
Any insights as to why the $scope.$on method may not be triggering?
Thank you in advance