1

Can I have replicate this switch statement behavior for Angular ng-switch directive anyway?

switch(variable){
 case 'sample1':
 case 'sample2':
 //So common functionality for both 'sample1' and 'sample2' could be written here
 break;
 case 'sample3':
 break;
}

In angular I tried using multiple ng-switch-when but it did not help.

I often come across scenarios where multiple cases options have share same implementations and I have to write redundant code for all those cases.

You feel there's a better practice I should follow then please share.

  • I considered using ng-if as one option.
  • I also tried transforming the case values ('sample1', 'sample2') to one common value (say 'sample_12_common' and hence write one match case in the snippet.

Thanks a lot.

Peter Parker
  • 358
  • 1
  • 4
  • 12

1 Answers1

0

There is a pull request, addressing your problem https://github.com/angular/angular.js/pull/3516

I have following ideas about workaround
1. write the evaluated expression so that it will have minimum number of different values that are interested for you.
2. place the most common behavior to default case

Alexey
  • 1,257
  • 7
  • 13