angularjs: multiple values in a ng-switch-when

前端 未结 7 1834
一向
一向 2021-02-11 11:59

I have the following ngSwitch:

Wrong

7条回答
  •  你的背包
    2021-02-11 12:50

    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

提交回复
热议问题