If I understood correctly, you have clot elements that should be hidden when a particular service loads data and then displays again when data is loaded, right?
In this case, events may be a good solution:
- they can be global for application (which I think is what you are looking for).
- they avoid direct communication between elements (also one of your problems).
So, in your service, just broadcasting events when something happens:
$rootScope.$broadcast('loading-data'); axajStuffOrWhatever(function() { $rootScope.$broadcast('data-loaded'); });
Then end the show / hide behavior in the directive that will listen to these events.
.directive('hideWhileLoadingData', function() { return { link: function(scope, el, attrs) { scope.$on('loading-data', function() { el.css('display', 'none'); }); scope.$on('data-ready', function() { el.css('display', 'block'); }); } }; });
Use the directive:
<div hide-while-loading-data>something</div>
The advantage of using events here is that they can later be created by another service or several services, and this directive will not be affected by this until the events are the same.
For more complex behavior, you can also parameterize events and the directive, so different elements will respond to different things.
I made an example of this.
gargc source share