javascript - ngSwitchCase no provider - Stack Overflow

I was watching turtorial and was trying to do what I learned.I got an error below.I couldn't under

I was watching turtorial and was trying to do what I learned.I got an error below.I couldn't understand why I have this error message on the browser console.It says [ERROR ->]<span *ngSwitchCase="true"> but I don't know why it says ngSwitchCase is wrong.Checked all files and code but seems no problem.Where's my mistake?

ERROR

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>
"):

appponent.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>

apponent.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;
   }
}

I was watching turtorial and was trying to do what I learned.I got an error below.I couldn't understand why I have this error message on the browser console.It says [ERROR ->]<span *ngSwitchCase="true"> but I don't know why it says ngSwitchCase is wrong.Checked all files and code but seems no problem.Where's my mistake?

ERROR

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.ponent.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.onent.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;
   }
}
Share Improve this question asked Jul 25, 2019 at 7:07 Timuçin ÇiçekTimuçin Çiçek 1,6263 gold badges24 silver badges51 bronze badges 2
  • tryto do like this: [ngSwitch]="{{item.action}}" – Shivani Sonagara Commented Jul 25, 2019 at 7:14
  • @ShivaniPatel I did but didn't work – Timuçin Çiçek Commented Jul 25, 2019 at 7:17
Add a ment  | 

3 Answers 3

Reset to default 7

You should wrap you span inside td tag like this because ngSwitch directive is scope to td element not outside td element

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

If you only have to decide between one of two alternatives, you could use *ngIf-else as well: https://ultimatecourses./blog/angular-ngif-else-then

you should apply [ngSwitch] on the parent element first to behave as a container to your switch cases and "provide" you with the condition you are checking/paring to, to apply/display any of your switch cases in the child elements which met the condition of the parent of any type(property/field/method/function).

so the parent element work as a "Provider"

ERROR: No provider for NgSwitch found in NodeInjector.

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

with provider & no errors:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745476259a4629361.html

相关推荐

  • javascript - ngSwitchCase no provider - Stack Overflow

    I was watching turtorial and was trying to do what I learned.I got an error below.I couldn't under

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信