Add null checks and made switchView directive robust.