Create a Template Storage Service in Angular

Share this video with your friends

Send Tweet

You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app.

gaurav soni
gaurav soni
~ 7 years ago

What are the actual use cases of directives? Mostly all the functionality is done via component. Then why should someone use directive? It would be very helpful if you provide some real application use case.

Viktor Soroka
Viktor Soroka
~ 7 years ago

@gaurav, I think the real meaning is to add additional behavior to the components without changing the component itself. By doing that you do not mess you component with the whole bunch of possible side scenarios and extract that scenarios to the directives. For example remember the 'track' example from this course, when the tracking logic was placed in the directive and not in the component itself thus making it lean and focused only on its own goal.