angular - How to use a typescript enum value in an Angular2 ngSwitch statement -


the typescript enum seems natural match angular2's ngswitch directive. when try use enum in component's template, "cannot read property 'xxx' of undefined in ...". how can use enum values in component template?

please note different how create html select options based upon of values of enum (ngfor). question ngswitch based upon particular value of enum. although same approach of creating class-internal reference enum appears.

you can create reference enum in component class (i changed initial character lower-case) , use reference template (plunker):

import {component} 'angular2/core';  enum celltype {text, placeholder} class cell {   constructor(public text: string, public type: celltype) {} } @component({   selector: 'my-app',   template: `     <div [ngswitch]="cell.type">       <div *ngswitchcase="celltype.text">         {{cell.text}}       </div>       <div *ngswitchcase="celltype.placeholder">         placeholder       </div>     </div>     <button (click)="settype(celltype.text)">text</button>     <button (click)="settype(celltype.placeholder)">placeholder</button>   `, }) export default class appcomponent {    // store reference enum   celltype = celltype;   public cell: cell;    constructor() {     this.cell = new cell("hello", celltype.text)   }    settype(type: celltype) {     this.cell.type = type;   } } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -