0

I have a fairly long ng-switch-when statement and some with repeating matches.

I've read that angular does not support multiple value ng-switch's (im using v1.2.8) but have found this article: How can I use ng-switch to satisfy multiple, same conditions? sharing how the library could be modifed.

I've gone through my angular library and found the ngSwitchWhenDirective:

var ngSwitchWhenDirective = ngDirective({
    transclude: 'element',
    priority: 800,
    require: '^ngSwitch',
    link: function(scope, element, attrs, ctrl, $transclude) {
          ctrl.cases['!' + attrs.ngSwitchWhen] = (ctrl.cases['!' + attrs.ngSwitchWhen] || []);
          ctrl.cases['!' + attrs.ngSwitchWhen].push({ transclude: $transclude, element: element });
    }
});

But can't see how this could be adapted as per the suggestion by angabriel:

config(function($routeProvider, $provide) {
/**
* overwrite angular's directive ngSwitchWhen
* can handle ng-switch-when="value1 || value2 || value3"
*/
$provide.decorator('ngSwitchWhenDirective', function($delegate) {
$delegate[0].compile = function(element, attrs, transclude) {
    return function(scope, element, attr, ctrl) {
      var subCases = [attrs.ngSwitchWhen];
      if(attrs.ngSwitchWhen && attrs.ngSwitchWhen.length > 0 && attrs.ngSwitchWhen.indexOf('||') != -1) {
      subCases = attrs.ngSwitchWhen.split('||');
    }
    var i=0;
    var casee;
    var len = subCases.length;
    while(i<len) {
        casee = $.trim(subCases[i++]);
        ctrl.cases['!' + casee] = (ctrl.cases['!' + casee] || []);
        ctrl.cases['!' + casee].push({ transclude: transclude, element: element });
    }
}
}
return $delegate;
});
});
Community
  • 1
  • 1
Oam Psy
  • 8,555
  • 32
  • 93
  • 157

0 Answers0