Como funciona o ngSwitch em Angular2?

estou a tentar usar o ngSwitch como neste exemplo mas eu tenho um erro:

enter image description here

O Meu Componente:

  template: `
    <div layout="column" layout-align="center center">   
   <div [ng-switch]="value">
       <div *ng-switch-when="'init'">
     <button md-raised-button class="md-raised md-primary">User</button>
     <button md-raised-button class="md-raised md-primary">Non user</button>

     </div>
  <div *ng-switch-when="0">Second template</div>
  <div *ng-switch-when="1">Third Template</div>
</div>
  </div>`,
    directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]
 })

o meu feiticeiro.ts

O que perdi? Obrigado.
Author: maniak1982, 2016-03-03

4 answers

Actualizar

Ver https://angular.io/api/common/NgSwitch

Original

Os modelos são sensíveis à capitalização (desde beta.47 AFAIR). Directive (attribute) selectors were changed to camel case. Por exemplo de ng-switch a ngSwitch.

Os nomes das marcas ainda usam traços para compatibilidade com os componentes web. Por exemplo <router-link>, <ng-content>.

Mais detalhes para ngSwitchCase

<container-element [ngSwitch]="switch_expression">
  <some-element *ngSwitchCase="match_expression_1">...</some-element>
</container-element>
<container-element [ngSwitch]="switch_expression">
  <some-element *ngSwitchCase="match_expression_1">...</some-element>
  <some-element *ngSwitchCase="match_expression_2">...</some-element>
  <some-other-element *ngSwitchCase="match_expression_3">...</some-other-element>
  <ng-container *ngSwitchCase="match_expression_3">
    <!-- use a ng-container to group multiple root nodes -->
    <inner-element></inner-element>
    <inner-other-element></inner-other-element>
  </ng-container>
  <some-element *ngSwitchDefault>...</some-element>
</container-element>
<container-element [ngSwitch]="switch_expression">
  <some-element *ngSwitchCase="match_expression_1">...</some-element>
  <some-other-element *ngSwitchDefault>...</some-other-element>
</container-element>
 29
Author: Günter Zöchbauer, 2017-07-11 11:18:12

à moda antiga


Demonstração De Trabalho. você pode verificar a consola do navegador

Alterado para ngSwitch
alterado ng-switch-quando para ngSwitchWhen

<div layout="column" layout-align="center center">

       <div [ngSwitch]="value">
      <div *ngSwitchWhen="'init'">
         <button md-raised-button class="md-raised md-primary">User</button>
         <button md-raised-button class="md-raised md-primary">Non user</button>

      </div>
      <div *ngSwitchWhen="0">Second template</div>
      <div *ngSwitchWhen="1">Third Template</div>
    </div>
 </div>
   <button md-fab
 class="md-fab wizard_button--next"
  aria-label="about"
  (click)="onNext()">
<i class="material-icons" md-icon="">play_arrow</i>
 </button>


Nova Forma

ANGULAR.2.0.0 ou Relase Final


Actualizar : Como Utilizar ngSwitch em Angular2. 0. 0 ou libertação final ???

Por favor, note que as coisas mudaram na versão final, por isso, se estiver a usar a versão final, por favor, veja abaixo um exemplo simples.

Demonstração simples: http://plnkr.co/edit/IXmUm2Th5QGIRmTFBtQG?p=preview

@Component({
  selector: 'my-app',
  template: `
  <button (click)="value=1">select - 1</button>
  <button (click)="value=2">select - 2</button>
  <button (click)="value=3">select - 3</button>
  <h5>You selected : {{value}}</h5>

  <hr>
  <div [ngSwitch]="value">

     <div *ngSwitchCase="1">1. Template - <b>{{value}}</b> </div>
     <div *ngSwitchCase="2">2. Template - <b>{{value}}</b> </div>
     <div *ngSwitchCase="3">3. Template - <b>{{value}}</b> </div>
     <div *ngSwitchDefault>Default Template</div>

  </div>
  `,

})
export class AppComponent {}
 34
Author: micronyks, 2016-10-05 06:28:25
Três coisas para ter em mente.ngSwitch, ngSwitchCase e ngSwitchDefault
  1. NgSwitch - define o property value de model. Por exemplo - viewMode, que é uma propriedade no seu componente.

  2. NgSwitchCase - define o que renderizar quando o value do property definido em ngSwitchChanges. Para ex. quando viewMode = 'map'

  3. NgSwitchDefault - define o que renderizar se o value não corresponder. Para ex. quando viewMode=undefined O padrão será rendered.

Outro ponto importanteé que precisamos de definir o [ngSwitchCase] dentro de um <template></template> elemento HTML caso contrário, não funcionará. Angular irá automaticamente convertê-lo numa etiqueta <div>.

<div [ngSwitch]="'viewMode'">
  <template [ngSwitchCase]="'map'" ngSwitchDefault> 
     Map View Content...   
   </template>      
   <template [ngSwitchCase]="'list'"> 
      List View Content...
  </template>
</div>
Boa Sorte.
 11
Author: Aakash, 2016-11-04 04:37:46
Há também o caso ngSwitchDefault que não vi na documentação no local. Assim como o nome implica que ele vai padrão para isso se ele não atender os outros casos. Isto é um aviso, se alguém se deparar com isto.
 5
Author: Christopher Whitehead, 2016-09-25 06:12:57