Clearly generalised code snippet and fixed call to angular.extend().