ngSwitchCase provider not found

While following a tutorial and trying to implement what I learned, I encountered an error that I'm having trouble understanding. The browser console shows an error message stating

[ERROR ->]<span *ngSwitchCase="true">
, but I can't figure out why it's flagging ngSwitchCase as incorrect. I've checked all the files and code multiple times, but I can't seem to find the mistake. What am I doing wrong?

Error Message:

Uncaught Error: Template parse errors:
No provider for NgSwitch ("       <td>{{item.description}}</td>
              <td [ngSwitch]="item.action"></td>
              [ERROR ->]<span *ngSwitchCase="true">
                  Yes
              </span>
"): ng:///AppModule/AppComponent.html@25:14
No provider for NgSwitch ("
                  Yes
              </span>
              [ERROR ->]<span *ngSwitchCase="false">
                  No
                </span>
"):

app.component.html

  <table class="table table-striped table-bordered">
                <thead>
                   <tr>
                     <th></th>
                     <th>Description</th>
                     <th>Action</th>
                   </tr>
                </thead>
                <tbody>
                <tr *ngFor="let item of items;let i=index">
                  <td>{{i+1}}</td>
                  <td>{{item.description}}</td>
                  <td [ngSwitch]="item.action"></td>
                  <span *ngSwitchCase="true">
                      Yes
                  </span>
                  <span *ngSwitchCase="false">
                      No
                    </span>
                </tr>
                </tbody>
              </table>

app.comonent.ts

export class AppComponent {

  items:Model[]= [
    new Model('Breakfast', false),
    new Model('Sport', false),
    new Model('Studying', true),
    new Model('Cemo', false),
  ]
}

Model.ts

export class Model {
   description:string;
   action:Boolean;
   
   constructor(description:string, action:Boolean) {
       this.description = description;
       this.action = action;
   }
}

Answer №1

For proper functionality, it is recommended to enclose the span within a td tag when using the ngSwitch directive, as it only works within the scope of the td element.

<td [ngSwitch]="item.action">
    <span *ngSwitchCase="true">
              Yes
    </span>
    <span *ngSwitchCase="false">
            No
    </span>
  </td>

Answer №2

In situations where you are faced with choosing between just two options, you can also utilize *ngIf-else:

Answer №3

To effectively utilize [ngSwitch], it is recommended to apply it on the parent element first, serving as a container for your switch cases. This will provide you with the necessary condition to check and display any of your switch cases in the child elements that meet the specified condition.

Essentially, the parent element acts as a "Provider" in this scenario.

An error may occur if the NgSwitch provider is not found in NodeInjector.

<div>
    <p *ngSwitchCase="true">
              Yes
    </p>
    <p *ngSwitchCase="false">
            No
    </p>
</div>

To avoid errors, ensure you incorporate the provider as shown below:

<div [ngSwitch]="something">
    <p *ngSwitchCase="true">
              Yes
    </p>
    <p *ngSwitchCase="false">
            No
    </p>
</div>

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

Creating a dual-direction infinite scroll effect with CSS through mouse dragging

I'm currently working on implementing an infinite scroll component for a project. After consulting this tutorial, I've encountered an issue. It seems that I can only achieve infinite scroll in one direction. Whenever I add elements to the leftmo ...

The functionality of the typeof operator is not behaving as anticipated

I am currently working on a script to verify the existence of a specific JavaScript object. var success = function(data) { var x= 0; var numOfCards = data.length; for (x=0;x<data.length - 1;x++) { if (typeof data[x].l ...

Error: Execution time limit of 30 seconds has been surpassed, causing a fatal issue on line 64

I am attempting to style text retrieved from a SQL database, but when I try to preview it, I encounter a Fatal error: Maximum execution time of 30 seconds exceeded on line 64 . I tried setting the maximum execution time to 300 with `ini_set('max_execu ...

The directive does not function properly when used across multiple files

Struggling with @Directives and @Hostlisteners - seeking assistance The directive added to the input seems unresponsive, failing to trigger any events or console.log(). I'm puzzled and frustrated as there appears to be a missing piece of the puzzle t ...

The first JSF page is not affected by the style sheet

I am facing an issue where the style sheet does not apply on my initial JSF page. The problem occurs when I have an index.jsp which forwards to my first JSF page. <html> <head></head> <body> <jsp:forward page="./start.js ...

Content OverFlow: DropDown Menu is not overlapping the content, but rather pushing it downwards

In my webpage, I have a drop-down menu that traditionally pushes the content below it down to make space for its items. However, I want the drop-down to overlap the contents below without affecting their position. I've tried various solutions, such a ...

I am currently having trouble with req.query not functioning correctly within Next.js for reading query parameters

I am facing an issue while working with a REST API in Next.js 13. I have created the API, which can be accessed at http://localhost:3000/api/portfolio. However, when I try to filter the data using query parameters like http://localhost:3000/api/portfolio?s ...

Send a pair of values using a set of two parameters

My current code is currently passing only one parameter value. However, I would like to modify it to pass two values with two parameters. This is my current code: <script> function getfilter(str){ document.getElementById("result"). ...

What can I do to prevent an anchor link from automatically scrolling to a different section of the page?

I've been attempting to prevent a page from scrolling after selecting an anchor link. Currently, the page layout includes a larger version of an image appearing in full view on the right panel after clicking on an image in the left panel (with text u ...

What steps do I need to take to integrate my RASA assistant into my personal website?

Deploying my rasa chatbot on my live website is my next step. While Rasa worked smoothly on my localhost server, as a newcomer to web development, I found the official guide provided by RASA in the link below a bit challenging to comprehend: The RASA guid ...

"Encountering an Invalid hook call error with React-Leaflet v4 and Next.js 13

I am facing an issue following my update of Next.js from version 12 to 13, which also involved updating React from 17 to 18 and react-leaflet from 3 to 4. Within this component: ` function ChangeView({ center }) { const map = useMap(); map.setView( ...

How come the Jquery :odd selector picks out the even elements?

<html> <head> <script type="text/javascript" src="jquery-1.7.1.js" ></script> <script type="text/javascript"> $(function() { $("p:odd").html('modified'); }); </script> </head> & ...

What could be causing the sorting function to malfunction on certain columns?

My table's column sorting feature works well with first name and last name, but it seems to have issues with dl and dl score columns. I need assistance in fixing this problem. To access the code, click here: https://stackblitz.com/edit/angular-ivy-87 ...

Having the same name for multiple query parameters does not result in an array being returned

Using the link http://example.com/users?test=1&test=2 router.route('/users/?').get((req, res) => { console.dir(req.query) //=> { test : 1 } }) The output is { test : 1 } instead of an expected array [ 1, 2 ]. Even ?test[]=1&test ...

Oops! Looks like there was an issue finding a matching route for the URL segment in Angular 2

I am currently exploring angular2 and trying to figure out how to incorporate multiple <router-outlets> in a specific template. Despite searching through numerous Q&A, I have been unable to resolve the issue. router.module.ts const routes: Routes = ...

Adding TypeScript types to an array within a function parameter: A step-by-step guide

Having some trouble defining the array type: The code below is functioning perfectly: const messageCustomStyles: Array<keyof IAlertMessage> = [ 'font', 'margin', 'padding' ]; r ...

Managing "unprocessed information" in a Node.js environment and transferring the information through a Node Express endpoint

Currently, I am in the process of making an API call to retrieve a file using axios: async function fetchData() { const configuration = {}; // { responseType: 'stream'}; const { response } = await axios.get(URL, configuration); c ...

The PHP script is failing to send out email notifications

I am facing an issue with my website's contact form that is supposed to send out an email after the user fills it out and clicks submit, but it does not appear to be working. Unfortunately, I do not have access to the mail server since it is hosted e ...

The collision between the Textfield Label and Value is being caused by Chrome's Autofill feature

When using React, I noticed that Autocomplete Chrome values don't trigger the onChange event right away. This leads to an issue where there is a collision between the MUI TextField Label and the actual values during the initial page load. How can I go ...

Tomickigrzegorz Script Finder Predictive Search

Hey there, I recently tried out the autocomplete script created by tomickigrzegorz. Check it out here! It's been working smoothly for me, but I'm facing an issue with external link tags like https//google.com not functioning properly. Here is ...