I have a controller as below, with an .$on
attribute that is called via .$broadcast
when a form is submitted. I'd also like the event to run when the controller loads. Is there a syntactically easy way to go about doing this, or will I have to add a on page load
listener?
myApp.controller('DownloadsCloudCtrl', ['$scope',
'$rootScope',
'requestService',
function($scope, $rootScope, requestService){
$scope.title = 'Most Popular Keywords';
$scope.tooltip = 'Test tooltip';
$rootScope.$on('updateDashboard', function(event, month, year) {
requestService.getP2PKeywordData(month, year).then(function(data) {
$scope.d3Data = data;
});
});
}]);
If you want to run this when your controller loads, then this is extremely simple. Basically remove your $on
logic into its own function and call it inside the controller init:
myApp.controller('DownloadsCloudCtrl', ['$scope', '$rootScope', 'requestService',
function($scope, $rootScope, requestService){
$scope.title = 'Most Popular Keywords';
$scope.tooltip = 'Test tooltip';
var updateDash = function(month, year) {
requestService.getP2PKeywordData(month, year).then(function(data) {
$scope.d3Data = data;
});
};
$rootScope.$on('updateDashboard', function(event, month, year) {
// run the update function
updateDash(month, year);
});
// run the update function once when the controller loads
updateDash(someMonth, someYear);
}]);
Now this would be much better abstracted as a service, but this should at least get you started.