Move code from controller to directive.