As in the comment, changing two things will fix the problem. The vectorization comes in through raster1[] and raster2[], so you need to modify the application of the function.
function(x,y){angle(x[],y[])})
and add a return at the end of the function angle():
return(theta)