Populating route search after dynamic routes have been added

Change-Id: I25ff31b3a016a020fc8c8b08f29ded700d01e203
diff --git a/src/app/core/header/header.ts b/src/app/core/header/header.ts
index 92d402d..88562ea 100644
--- a/src/app/core/header/header.ts
+++ b/src/app/core/header/header.ts
@@ -13,7 +13,7 @@
 }
 
 class HeaderController {
-  static $inject = ['$scope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService'];
+  static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'NavigationService'];
   public notifications: INotification[] = [];
   public newNotifications: INotification[] = [];
   public version: string;
@@ -24,6 +24,7 @@
 
   constructor(
     private $scope: angular.IScope,
+    private $rootScope: ng.IScope,
     private $state: IStateService,
     private authService: IXosAuthService,
     private syncStore: IStoreService,
@@ -44,8 +45,7 @@
       // tapToDismiss: false
     });
 
-    // TODO set a global event after routes have been loaded
-    window.setTimeout(() => {
+    this.$rootScope.$on('xos.core.modelSetup', () => {
       this.states = this.NavigationService.query().reduce((list, state) => {
         // if it does not have child (otherwise it is abstract)
         if (!state.children || state.children.length === 0) {
@@ -60,7 +60,7 @@
         return list;
       }, []);
       this.states = _.uniqBy(this.states, 'state');
-    }, 500);
+    });
 
     // listen for keypress
     $(document).on('keyup', (e) => {
@@ -71,7 +71,6 @@
 
     // redirect to selected page
     this.routeSelected = (item: IXosNavigationRoute) => {
-      console.log(`go to: ${item.state}`);
       this.$state.go(item.state);
       this.query = null;
     };