refactor($compile): move function def out of loop