I have three buttons that shows three different lists, so I wanted to add enum
s 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.
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
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.
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:
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.
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
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With