Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ngSwitch in angular2

Tags:

angular

Since last two days I am trying to get ngSwitch to work in Angular 2.1.0. But it seems impossible to get it work.

I always get:

No provider for NgSwitch

Below is my code:

Template:

<template [ngSwitch]="buttonSelector">
  <a class="btn" [ngClass]="buttonSizeClass" *ngSwitchCase="'link'" href="#">
    <span class="btn__text">
      <ng-content></ng-content>
    </span>
  </a>
</template>

Component:

import { Component, OnInit, Input } from '@angular/core';


@Component({
  selector: 'app-inked-btn',
  templateUrl: './inked-btn.component.html',
  styleUrls: ['./inked-btn.component.css'],
  inputs: ['buttonSize', 'buttonType', "buttonSelector"]
})
export class InkedBtnComponent implements OnInit {
  public buttonSize: string;
  public buttonType: string;
  public buttonSelector: string;
  public buttonSizeClass: any;

  constructor() { }

  ngOnInit() {
    this.buttonSizeClass = {
      'btn--lg': this.buttonSize === 'large',
      'btn--sm': this.buttonSize === 'small',
      'btn--primary': this.buttonType === 'primary'
    }
  }

}

Below is my module configuration:

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { InkedBtnComponent } from './inked-btn/inked-btn.component';

@NgModule({
  imports: [
    CommonModule,
    RouterModule
  ],
  declarations: [HeaderComponent, FooterComponent, InkedBtnComponent],
  exports: [HeaderComponent, FooterComponent, InkedBtnComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class SharedModule { }

This shared module is then imported in the main module.

Where is the miss?

like image 360
Abhishek Prakash Avatar asked Dec 11 '16 10:12

Abhishek Prakash


People also ask

What is the use of ngSwitch?

The [ngSwitch] directive on a container specifies an expression to match against. The expressions to match are provided by ngSwitchCase directives on views within the container. Every view that matches is rendered. If there are no matches, a view with the ngSwitchDefault directive is rendered.

What is * NgSwitchCase?

NgSwitchCaselinkProvides a switch case expression to match against an enclosing ngSwitch expression. When the expressions match, the given NgSwitchCase template is rendered. If multiple match expressions match the switch expression value, all of them are displayed.

What are the three commands used while working with Ng switch directive?

It uses three keywords as follows. ngSwitch: We bind an expression to it that returns the switch value. It uses property binding. ngSwitchCase: Defines the element for matched value.


1 Answers

https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html

ngSwitch can't be on a <template> element, only on real elements like <div>

Only ngSwitchCase can be applied to <template> elements

<template [ngSwitchCase]="match_expression_3">

Alternatively since final ng-container can be used instead of <template> with the more common syntax:

<ng-container *ngSwitchCase="match_expression_3">
like image 124
Günter Zöchbauer Avatar answered Oct 24 '22 05:10

Günter Zöchbauer