Refactored angular code to use angular directive