I have an issue with my angularjs app on the events and listeners.
My application has an index.html file like this:
<body ng-app="ua.myApp">
<!-- Navigation bar -->
<ng-include src="'app/common/navbar/navbar.tpl.html'"></ng-include>
<ng-view></ng-view>
<script type="text/javascript" src="app/common/navbar/navbar.js"></script>
<script type="text/javascript" src="app/part1/part1.js"></script>
</body>
In the navbar controller I have a listener:
console.log('Setup event listner - navBar: update');
$scope.$on('navBar: update', function() {
if (uaContext.loginStatus.get() == true) {
$scope.setLoggedInBar();
} else {
$scope.setLoggedOutBar();
}
});
And in the part1 app I broadcast an event:
function ($scope, $rootScope, $routeParams, uaContext) {
console.log('Send event listner - navBar: update');
$scope.$on('$routeChangeSuccess', function () {
uaContext.productId.set($routeParams.productId);
uaContext.appName.set('part 1');
$rootScope.$broadcast('navBar: update');
});
}
The dependencies in myApp are in this order:
var myApp = angular.module('ua.myApp', [
'ua.NavBar',
'ua.Part1']);
It's working fine. Console log:
Setup event listner - navBar: update (nav_bar.js)
Send event listner - navBar: update (part1.js)
The issue is that sometimes the event is sent by part1 app before the listener in navbar is operational. So we get this situation:
Send event listner - navBar: update (part1.js)
Setup event listner - navBar: update (nav_bar.js)
Thus the nav bar is not updated.
Do you know how I can fix this issue? Maybe I can emit the event in another event than routeChangeSuccess but I didn't found the documentation on the events.
Thank you