refactor code with angularjs