Convert vanilla JS to AngularJS