Refactor to be used inside of an Angular directive