Is this a common practice?
Yes, this is common practice, directives often need access to the $ scope variable for variables and calls (e.g. LoadMoreTweets ).
Even in official documents , they have examples like ..
scope.$watch(...)
... fom inside the directive (where the controller can make changes to the observed variable).
This is quite appropriate for the directive to communicate with the controller in this way.
You can even enter the controller itself (and not just the area) into the link method of the directive. Take a look at the “Function Binding” section of http://docs.angularjs.org/guide/directive .
"The controller is common to all directives, which allows directives to use controllers as a communication channel."
But is it possible to use the service?
Of course. The question is why? If you work with a single page (for example, displays tweets), and the controller already has the variable $scope.tweets and $scope.loadMoreTweets , you over-click on things if you try to use the service where it is not needed.
Services, on the other hand, are ideal for widespread data such as user profiles. If your directive needs to access the current user, then it makes sense to do this through the service.
From the docs:
Services
Angular are singletones that perform specific tasks common to web applications, such as the $ http service, which provides low-level access to the browser XMLHttpRequest object.
If you have a task for the whole application that is best suited for a single, be sure to use the service. If you just need to access the $ scope variable from a directive, just talk directly to the controller or raise an event that the controller can respond to.
jszobody
source share