refactor part of codes using Angular.js