Encountering the error message "TemplateRef provider not found" when using Angular2 (2.0.0-beta.17) NgSwitchWhen

I've hit a roadblock with this problem and after spending quite a few hours on it, I need one of you to point out where I'm going wrong :)

The issue I'm facing is that the component below is throwing an error saying 'No provider for TemplateRef!'. You can check out the Plunker example here. The ngSwitch syntax that I used is from here.

import {Component} from '@angular/core'
//I thought importing CORE_DIRECTIVES would make the NgSwitch family available, but no luck.
import {CORE_DIRECTIVES} from '@angular/common'
//I also tried explicitly importing NgSwitch and NgSwitchWhen separately

@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]
  //I also tried using directives: [NgSwitch,NgSwitchWhen] instead
})
export class App {
  switchValue = 42;
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}

Answer №1

When utilizing the concise syntax with implicit <template>, remember to include *

@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!)'
  }
}

For more information, refer to the NgSwitch directive documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the RxJS operator that corresponds to the combination of observableX and observableY being subscribed to asynchronously in a template?

In my component template, I currently have this code snippet: <mat-spinner *ngIf="((facade.user.data$.isLoading | async) || (facade.product.data$.isLoading | async))"></mat-spinner> My goal is to consolidate this union into a single ...

Utilize ngx-translate in Ionic 2 for translating menu items

I have successfully implemented ngx-translate for multi-language support in my application. However, I am now looking to extend this functionality to my menu items. How can I achieve this for my 3 menu items with different titles? ts file appPages: Pag ...

What is the best way to incorporate interface validation in TypeScript switch statements?

Currently utilizing Typescript and redux My goal is to compare a passed action with an interface, then execute the appropriate task export function counterReducer( state = initialState, action: CounterActionTypes ): CounterState { switch (action) { ...

Having difficulty leveraging npm modules in TypeScript

I recently switched from Babel to Typescript and am facing difficulties with importing a module from node_modules. The generated .js build does not include the code from the module I'm trying to import, specifically browser-cookies. I used yarn to in ...

Instructions for populating the primeng <p-datatable> using FirebaseObservableList

I am attempting to populate my data table with content from my Firebase database. However, I am unsure on how to utilize the *ngFor directive as I would in a regular table. The conventional method is as follows: <tr *ngFor="let docu of documentos | asy ...

Get items from an array that have a property matching another array

Is there a more efficient way to create a new array with contact objects that have values matching those in selectedContact? selectedContact: number[] = [0,2] //value contacts: Contact[] = [{ firstName:"Dan"; lastName:"Chong"; email:"<a href="/c ...

An error was encountered while attempting to redirect in an Angular 4 project, the URL was set up for HTTPS

I've been attempting to access an Angular 4 project deployed locally on my machine through a form on an HTML page: <!DOCTYPE html> <html> <body> <form action="https://test-pubg.sohum.com:4200" target="_blank" method="post"> ...

tips for incorporating datatable into ng bootstrap modal within an angular application

I am trying to create a data table within an ng-bootstrap modal in Angular using Bootstrap and the angular-data tables module. My goal is to display the data table specifically within a bootstrap modal. ...

Stop material button from animating when clicked

On my webpage, I have created a unique button structure using Angular Material design: <button mat-button class="library-tile-button"> <button mat-button class="edit-library-button"> <img class="edit-library-img" src="..."/> ...

Retrieve the value of a property within the same interface

Is there a way to access an interface prop value within the same interface declaration in order to dynamically set types? I am attempting something like this: export type MethodNames = "IsFallmanagerUpdateAllowed" | "UpdateStammFallmanager& ...

How can I ensure thorough test coverage without relying on Testbed?

We have implemented some custom form control components with decorators as follows: @Component({ selector: 'value-selector', templateUrl: './selector.component.html', styleUrls: ['./selector.component.scss'], provid ...

Angular Command Line Interface version 12.2.0 displays the message: "No overload matches this call."

I've been working on developing a shopping cart feature for an Angular project. I'm trying to define a product as a Product type, but I keep encountering an error message stating: "(product: Product) => { ..." The error message rea ...

Troubleshooting Angular Reactive Forms: Issue with Binding Dynamic Select Dropdown Value

I currently have two arrays of data: AssociatedPrincipals, which contains previously saved data, and ReferencePrincipals, which consists of static data to populate dropdown controls. I am facing difficulties in displaying/selecting the previous value from ...

Customize the datepicker locale in Valor

Currently, I am working with Angular2 and typescript alongside the Valor datepicker. Unfortunately, the datepicker does not have the necessary locale built-in. After some research, I discovered that the essential JavaScript file containing the locale infor ...

Issue with Angular 2: Unable to locate the module '@angular/animations/browser'

I am currently attempting to install Angular2-Toaster (https://github.com/Stabzs/Angular2-Toaster). Initially, I successfully installed it using npm install angular2-toaster and was able to import it without any errors. However, I realized that I also need ...

Error Encountered: Visual Studio cannot locate the file 'COMPUTE_PATHS_ONLY.ts' during the build process

Upon fixing my visual studio 2015, an error was thrown that I haven't encountered before. Error Build: File 'COMPUTE_PATHS_ONLY.ts' not found. I did not add COMPUTE_PATHS_ONLY.ts to my Git repository. The other files in the repo rema ...

Tips for creating a state change function using TypeScript

Imagine the temperature remains constant for 20 minutes, but at the 21st minute, it changes. The state change is determined by a programmable state change function. How can I create a function to calculate the difference in state change? if(data.i ...

Is there a way to make PrismaClient return DateTime fields as Unix timestamps rather than JavaScript date objects?

When utilizing the PrismaClient for database interaction, DateTime fields are returned as JavaScript Date objects instead of Unix timestamp numbers. Despite being stored as Unix timestamp numbers in the database itself, I require the dates to be retrieved ...

Is there a way to specify separate paths for i18n resources in Angular depending on whether it's for the development or

While working on my development version of the Angular application, all paths are correct. However, when I build the application for production and deploy it to /frontend/, the paths seem to change. In order to modify the path to i18n resources (using ngx ...

Unable to employ pixelRatio for canvas within react three fiber

I'm working on a 3D model using react-three-fiber and I need to make it responsive. Unfortunately, the model appears blurry on smaller screens so I tried using pixelRatio = {window.devicePixelRatio}. However, I encountered an issue as the prop pixelRa ...