angularjs: multiple values in a ng-switch-when

前端 未结 5 1886
不知归路
不知归路 2021-02-11 12:03

I have the following ngSwitch:

Wrong

5条回答
  •  -上瘾入骨i
    2021-02-11 12:31

    This cannot be achieved with angular's base directives, but if you like, you could write your own directive to implement this, and could already interface with the existing ngSwitch directive.

    ngSwitchController has one property cases which is a map. Every case key is prefixed with an ! and the default case is equal to ?. Each case value is an object with two properties: transclude and element.
    Warning: Unlike ngModelController, ngSwitchController is not published API, so it's subject to change.

    Based off of the original ngSwitchWhenDirective, we can construct a multiswitchWhen, that will work with all existing ngSwitch, ngSwitchWhen, and ngSwitchDefault directives without conflict.

    .directive('multiswitchWhen', function () {
        return {
            transclude: 'element',
            priority: 800,
            require: '^ngSwitch',
            link: function(scope, element, attrs, ctrl, $transclude) {
                var selectTransclude = { transclude: $transclude, element: element };
                angular.forEach(attrs.multiswitchWhen.split('|'), function(switchWhen) {
                    ctrl.cases['!' + switchWhen] = (ctrl.cases['!' + switchWhen] || []);
                    ctrl.cases['!' + switchWhen].push(selectTransclude);
                });
            }
        }
    });
    

    Example plunker: http://plnkr.co/edit/K9znnnFiVnKAgGccSlrQ?p=preview

提交回复
热议问题