@@ -462,28 +462,31 @@ angularWidget('ng:include', function(element){
462462 * Conditionally change the DOM structure.
463463 *
464464 * @usageContent
465- * <any ng:switch-when="matchValue1"/ >...</any>
466- * <any ng:switch-when="matchValue2"/ >...</any>
465+ * <any ng:switch-when="matchValue1">...</any>
466+ * <any ng:switch-when="matchValue2">...</any>
467467 * ...
468- * <any ng:switch-when="matchValueN"/ >...</any>
468+ * <any ng:switch-default >...</any>
469469 *
470470 * @param {* } on expression to match against <tt>ng:switch-when</tt>.
471471 * @paramDescription
472472 * On child elments add:
473473 *
474474 * * `ng:switch-when`: the case statement to match against. If match then this
475475 * case will be displayed.
476+ * * `ng:switch-default`: the default case when no other casses match.
476477 *
477478 * @example
478479 <select name="switch">
479480 <option>settings</option>
480481 <option>home</option>
482+ <option>other</option>
481483 </select>
482484 <tt>switch={{switch}}</tt>
483485 </hr>
484486 <ng:switch on="switch" >
485487 <div ng:switch-when="settings">Settings Div</div>
486488 <span ng:switch-when="home">Home Span</span>
489+ <span ng:switch-default>default</span>
487490 </ng:switch>
488491 </code>
489492 *
@@ -495,6 +498,10 @@ angularWidget('ng:include', function(element){
495498 * select('switch').option('home');
496499 * expect(element('.doc-example ng\\:switch').text()).toEqual('Home Span');
497500 * });
501+ * it('should select deafault', function(){
502+ * select('switch').option('other');
503+ * expect(element('.doc-example ng\\:switch').text()).toEqual('default');
504+ * });
498505 */
499506var ngSwitch = angularWidget ( 'ng:switch' , function ( element ) {
500507 var compiler = this ,
@@ -505,21 +512,26 @@ var ngSwitch = angularWidget('ng:switch', function (element){
505512 changeExpr = element . attr ( 'change' ) || '' ,
506513 cases = [ ] ;
507514 if ( ! usingFn ) throw "Using expression '" + usingExpr + "' unknown." ;
515+ if ( ! watchExpr ) throw "Missing 'on' attribute." ;
508516 eachNode ( element , function ( caseElement ) {
509517 var when = caseElement . attr ( 'ng:switch-when' ) ;
510- if ( when ) {
511- cases . push ( {
512- when : function ( scope , value ) {
513- var args = [ value , when ] ;
514- foreach ( usingExprParams , function ( arg ) {
515- args . push ( arg ) ;
516- } ) ;
517- return usingFn . apply ( scope , args ) ;
518- } ,
518+ var switchCase = {
519519 change : changeExpr ,
520520 element : caseElement ,
521521 template : compiler . compile ( caseElement )
522- } ) ;
522+ } ;
523+ if ( isString ( when ) ) {
524+ switchCase . when = function ( scope , value ) {
525+ var args = [ value , when ] ;
526+ foreach ( usingExprParams , function ( arg ) {
527+ args . push ( arg ) ;
528+ } ) ;
529+ return usingFn . apply ( scope , args ) ;
530+ } ;
531+ cases . unshift ( switchCase ) ;
532+ } else if ( isString ( caseElement . attr ( 'ng:switch-default' ) ) ) {
533+ switchCase . when = valueFn ( true ) ;
534+ cases . push ( switchCase ) ;
523535 }
524536 } ) ;
525537
@@ -532,10 +544,12 @@ var ngSwitch = angularWidget('ng:switch', function (element){
532544 return function ( element ) {
533545 var scope = this , childScope ;
534546 this . $watch ( watchExpr , function ( value ) {
547+ var found = false ;
535548 element . html ( '' ) ;
536549 childScope = createScope ( scope ) ;
537550 foreach ( cases , function ( switchCase ) {
538- if ( switchCase . when ( childScope , value ) ) {
551+ if ( ! found && switchCase . when ( childScope , value ) ) {
552+ found = true ;
539553 var caseElement = quickClone ( switchCase . element ) ;
540554 element . append ( caseElement ) ;
541555 childScope . $tryEval ( switchCase . change , element ) ;
@@ -550,7 +564,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
550564 } ;
551565} , {
552566 equals : function ( on , when ) {
553- return on == when ;
567+ return '' + on == when ;
554568 } ,
555569 route : switchRouteMatcher
556570} ) ;
0 commit comments