Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to use enum properly with ngSwitch

I have three buttons that shows three different lists, so I wanted to add enums to help me decide which list to present using ngSwitch, but I'm getting an error.

This is the TypeScript:

export enum ListType {People, Cars}

export class AppCmp implements OnInit {

    listOfPeople: Person[];
    listOfCars: Car[];

    currentListView: CurrentListView;

    constructor(private _MyService: MyService) {
    };


    public setListType(type: ListType) {
        this.listType = type;
    }

    ngOnInit() {
      this._MyService.getListOfPeopleData().subscribe(res => {
        this.listOfPeople = res;
      });

      this._MyService.getListOfCarsData().subscribe(res => {
        this.listOfCars = res;
      });
    }
}

This is the HTML:

<div>
  <button md-button
          (click)="setListType(listType.People)"
          class="md-primary">People
  </button>

  <button md-button
          (click)="setListType(listType.Cars)"
          class="md-primary">Cars
  </button>
</div>


<md-content>

  <h1 align="center">{{title}}</h1>

  <div [ngSwitch]="currentListView">

    <div *ngSwitchCase="listType.People">
        <div class="list-bg" *ngFor="#person of listOfPeople">
          ID: {{person.id}} <p></p> name:{{person.name}}
        </div>
      </div>
    </div>
    <div *ngSwitchCase="listType.Cars">
        <div class="list-bg" *ngFor="#car of listOfCars;>
          ID: {{car.id}} <p></p> color: {{car.color}}
        </div>
    </div>

  </div>

</md-content>

What am I doing wrong here?

The error is:

EXCEPTION: Error: Uncaught (in promise): Template parse errors: Can't
bind to 'ngSwitchCase' since it isn't a known native property ("  
  <div [ngSwitch]="currentListView">

     <div [ERROR ->]*ngSwitchCase="listType.People"> Property binding ngSwitchCase not used
by any directive on an embedded template ("  
  <div [ngSwitch]="currentListView">

I'm using Typescript and Angular2.

like image 842
jack miao Avatar asked Jul 30 '16 22:07

jack miao


People also ask

Can We now reference enum values in ngswitchcase directive?

We can now reference the enum value in each of the NgSwitchCase directive: This is much cleaner as we are not using "magic" hard-coded strings in our templates and are referencing the single Department enumeration type. I finished! On to the next chapter

Can I enumerate ngswitch with typescript?

Rather, we can specify an expression to each NgSwitchCase directive that is evaluated against the expression that is provided to the NgSwitch root directive. This is well suited for an enumeration with TypeScript. If you're new to TypeScript, or come from a pure JavaScript background, you may not be familar with enumerations.

How to use enums in component templates?

Then in your component, you bring in the Enum type via a member 'MyEnum', and create another member for your enum variable 'myEnumVar' : export class MyComponent { MyEnum = MyEnum; myEnumVar:MyEnum = MyEnum.Second ... } You can now use myEnumVar and MyEnum in your .html template. Eg, Using Enums in ngSwitch:

What is the syntax of ngswitch?

Syntax of ngSwitch. ngSwitch. ngSwitchCase. The inner elements are placed inside the container element. The ngSwitchCase directive is applied to the inner elements with a match expression.Whenever the value of the match expression matches the value of the switch expression , the corresponding inner element is added to the DOM.


1 Answers

Considering the enum:

export enum ListType {People, Cars}

If you want to use it in the template, like:

...
<div *ngSwitchCase="listType.People">
...

You have to make the enum available to your component by creating a property in your class that will act as an "alias" (in that template) for the enum, like this:

export class AppCmp implements OnInit {

  public listType = ListType;  // <-- a prop with the name you want to use for the enum
...
like image 196
acdcjunior Avatar answered Sep 22 '22 12:09

acdcjunior