Call close function when scope is destroyed (updated)

When using this directive, I ran into a scenario where the scope of the context menu would be destroyed before the click event handler would be triggered, and as such the opened variable for some reason would be false, which meant the context menu wouldn't close.

Specifically, I use the context menu to be able to remove items from a list. And when removing the last item in that list, the menu would be left open. If the close function was called when the scope was destroyed however, things work as expected.

**Update**: I had to make sure that menuElement was defined in the close function.