How can I use ng-switch to satisfy multiple, same conditions?

后端 未结 3 822
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 09:14

I want to create a piece of HTML with AngularJS based on the authentication status.

I use ng-switch.

The issue is that I want to have multiple hits

相关标签:
3条回答
  • 2021-01-23 09:36

    Unfortunately the ng-switch directive only supports multiple matches since version 1.1.3 of AngularJS.

    See the changelog and this commit as reference.

    0 讨论(0)
  • 2021-01-23 09:48

    Someone overwrote the ngSwitchWhen Directive to include the option to allow || operators, you can find the original response here: http://docs.angularjs.org/api/ng.directive:ngSwitch 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;
      });
    });
    
    0 讨论(0)
  • 2021-01-23 09:51

    Angular 1.1.5 supports ng-if. You can look at that. Then you can have independent conditions.

    0 讨论(0)
提交回复
热议问题