moving every piece of js function into Angular's controller