0

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;
    });
  });
}]);
sir_thursday
  • 5,270
  • 12
  • 64
  • 118
  • What do you mean by *when the page loads for the first time*? Why not just run your desired code when the controller loads? – Matt Way Jun 26 '14 at 00:03
  • Sure, sorry that is a better way of putting it. I'd like it to run when the controller loads. – sir_thursday Jun 26 '14 at 00:04
  • Same question as Matt. Couldn't you just broadcast when you load the controller? – JerryKur Jun 26 '14 at 00:04
  • Oh I see... is that the best way to solve it? If so then thanks – sir_thursday Jun 26 '14 at 00:05
  • 1
    It seems pretty clean to use the broadcast. I would probably consider putting the broadcast in a service that all of the controllers could share. But that is more a style issue. – JerryKur Jun 26 '14 at 00:10
  • Okay. If I add a `broadcast` line inside the controller, I'll have to call it with the `$cookieStore` values since the `broadcast` isn't inside the form. Kind of lame, extra code than I would've wanted, but it seems like it's the best way (outside of creating a whole new service). – sir_thursday Jun 26 '14 at 00:11
  • Well, here is another question like yours [angular on pageload](http://stackoverflow.com/questions/15458609/angular-js-how-to-execute-function-on-page-load) – Tyler.z.yang Jun 26 '14 at 00:20

1 Answers1

1

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.

Matt Way
  • 32,319
  • 10
  • 79
  • 85