Updated to deal with the race conditions in the controller and the directive.