refactor to AngularJS code