Maintaining Separation of Concerns Through Angular Directives
We have a date picker component in our application. We send an event each time a user changes the date to our analytics provider. We have one use of it so far, so the analytics code was inside the component that uses it:
However, now we have more places where we must use this code, and we don’t want to duplicate it everywhere. One might think that the logic could be incorporated into the date picker component and passed as input:
Indeed, that would work, but it’s not the ideal design. Separation of concerns means that the date picker component isn’t concerned with the analytics and doesn’t need to know anything about it.
Furthermore, in this case, we have control over the source code since it’s an internal component, but what if it was a third-party component?
A better option here would be to use Angular directives. Let’s create a directive that obtains a reference to the form control via DI, subscribes to value changes, and fires the analytics event:
We can now use it every time we use the date picker:
Follow me on Medium or Twitter to read more about Angular, and JS!