Sitemap
Danny Fenstermaker

Danny Fenstermaker

UI Engineer Manager and tech lead

Angular Directive Communication using promises

2 min readSep 13, 2014

--

Being fairly new to Angular and thus learning the in’s and out’s of the framework I recently came across the need for cross Directive communication. My first shot at this was using the app.$emit and I felt like this was not isolating the functionality of communication enough. With that I was looking at other options and decided to use something along the lines of the mediator pattern.

Angular being what it is I looked around and decided to use a mediator directive and matcher or child directives that register with the mediator assuming it’s in scope.

Press enter or click to view image in full size

These child directives register with the mediator and are returned a promise. In our use case we needed to see if another child/matcher directive had the same properties. Assuming a match is found the two directives have access to the same resolved promise and get the info they need. While our use case is simple any comparison or resolution case can be fulfilled.

Get Danny Fenstermaker’s stories in your inbox

Join Medium for free to get updates from this writer.

This seemed cleaner to me simply because it isolates the functionality of a given element and allows the element to never have to know it was registering with the mediator.

Press enter or click to view image in full size

I know there is room for cleanup here and that this is abusing the idea of promises but the cleanliness of the end result was the lesser of the evils. That being said I would enjoy hearing peoples opinions and how this may have been better handled. Thanks for reading!

--

--