From 0af172040e03811c59d01682968241e3df226774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Galfas=C3=B3?= Date: Wed, 6 Feb 2013 10:08:40 -0300 Subject: [PATCH] feat(ngSwitch): support multiple matches on ngSwitchWhen and ngSwitchDefault Closes #1074 --- src/ng/directive/ngSwitch.js | 43 ++++++++++++++++++----------- test/ng/directive/ngSwitchSpec.js | 45 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/ng/directive/ngSwitch.js b/src/ng/directive/ngSwitch.js index 7b6981077264..aa04a9981521 100644 --- a/src/ng/directive/ngSwitch.js +++ b/src/ng/directive/ngSwitch.js @@ -20,8 +20,11 @@ * On child elments add: * * * `ngSwitchWhen`: the case statement to match against. If match then this - * case will be displayed. - * * `ngSwitchDefault`: the default case when no other casses match. + * case will be displayed. If the same match appears multiple times, all the + * elements will be displayed. + * * `ngSwitchDefault`: the default case when no other case match. If there + * are multiple default cases, all of them will be displayed when no other + * case match. * * @example @@ -69,22 +72,28 @@ var ngSwitchDirective = valueFn({ }], link: function(scope, element, attr, ctrl) { var watchExpr = attr.ngSwitch || attr.on, - selectedTransclude, - selectedElement, - selectedScope; + selectedTranscludes, + selectedElements, + selectedScopes = []; scope.$watch(watchExpr, function ngSwitchWatchAction(value) { - if (selectedElement) { - selectedScope.$destroy(); - selectedElement.remove(); - selectedElement = selectedScope = null; + for (var i= 0, ii=selectedScopes.length; i' + + '
  • first:{{name}}
  • ' + + '
  • , first too:{{name}}
  • ' + + '
  • second:{{name}}
  • ' + + '
  • true:{{name}}
  • ' + + '')($rootScope); + expect(element.html()).toEqual('' + + '' + + '' + + ''); + $rootScope.select = 1; + $rootScope.$apply(); + expect(element.text()).toEqual('first:, first too:'); + $rootScope.name="shyam"; + $rootScope.$apply(); + expect(element.text()).toEqual('first:shyam, first too:shyam'); + $rootScope.select = 2; + $rootScope.$apply(); + expect(element.text()).toEqual('second:shyam'); + $rootScope.name = 'misko'; + $rootScope.$apply(); + expect(element.text()).toEqual('second:misko'); + $rootScope.select = true; + $rootScope.$apply(); + expect(element.text()).toEqual('true:misko'); + })); + + it('should switch on switch-when-default', inject(function($rootScope, $compile) { element = $compile( '' + @@ -50,6 +80,21 @@ describe('ngSwitch', function() { })); + it('should show all switch-when-default', inject(function($rootScope, $compile) { + element = $compile( + '
      ' + + '
    • one
    • ' + + '
    • other
    • ' + + '
    • , other too
    • ' + + '
    ')($rootScope); + $rootScope.$apply(); + expect(element.text()).toEqual('other, other too'); + $rootScope.select = 1; + $rootScope.$apply(); + expect(element.text()).toEqual('one'); + })); + + it('should call change on switch', inject(function($rootScope, $compile) { element = $compile( '' +