ctrl.selected added to check where it's undefined

When you have arrays coupled to ng-model that are null or undefined, you get errors when using ctrl.selected that also is undefined.

Added an extra check in the if-statements to be sure that ctrl.selected isn't null or undefined.