Como funciona o ngSwitch em Angular2?
estou a tentar usar o ngSwitch como neste exemplo mas eu tenho um erro:
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 que perdi? Obrigado.
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>
à 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 {}
ngSwitch
, ngSwitchCase
e ngSwitchDefault
NgSwitch - define o
property value
demodel
. Por exemplo -viewMode
, que é uma propriedade no seu componente.NgSwitchCase - define o que renderizar quando o
value
doproperty
definido emngSwitchChanges
. Para ex. quandoviewMode = 'map'
NgSwitchDefault - define o que renderizar se o
value
não corresponder. Para ex. quandoviewMode=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.