4

I've been stumped by this for a fair few hours now. It's time for one of you guys to tell me what I'm doing wrong :)

The below component fails with 'No provider for TemplateRef!'. A plunker is available here. I got the ngSwitch syntax from here.

import {Component} from '@angular/core'
//This should make the NgSwitch family available, right?
import {CORE_DIRECTIVES} from '@angular/common'
// I get same result with explicit import
// import {NgSwitch,NgSwitchWhen} from '@angular/common' 

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <div [ngSwitch]="switchValue">
        <div ngSwitchWhen="42">It's 42!</div>
        <div ngSwitchWhen="21">It's 21!</div>
      </div>
    </div>
  `,
  directives: [CORE_DIRECTIVES]
  // directives: [NgSwitch,NgSwitchWhen]
})
export class App {
  switchValue = 42;
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}
LOAS
  • 7,161
  • 2
  • 28
  • 25

1 Answers1

10

If you use the short syntax with implicit <template> you need to add *

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <div [ngSwitch]="switchValue">
        <div *ngSwitchCase="42">It's 42!</div>
        <div *ngSwitchCase="21">It's 21!</div>
        <!-- before RC.2 
          <div *ngSwitchWhen="42">It's 42!</div>
          <div *ngSwitchWhen="21">It's 21!</div>
        -->
      </div>
    </div>
  `,
})
export class App {
  switchValue = 42;
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}

See also NgSwitch directive

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    My hero! I had already tried various combinations of [..] and *.., but not the one you suggested :) I am just now learning angular2, so I haven't quite familiarized myself with the explicit and implicit – LOAS May 05 '16 at 09:41