typescriptenumstypescript2.0

How to use enum in typescript?


I have the following enum:

export enum SubstitutionType {
  REPLACEMENT_TEACHER_SUBJECT = 0,
  REPLACEMENT_TEACHER = 1,
  REPLACEMENT_FREE = 2
}

I try to user this in class like this:

class A {

public substitutionsType: SubstitutionType;

}

Then in template I can not get access to substitutionsType like this:

substitutionsType.REPLACEMENT_FREE

Why, how to use this?


Solution

  • Hope this help you:

    export enum SubstitutionType {
      REPLACEMENT_TEACHER_SUBJECT,
      REPLACEMENT_TEACHER,
      REPLACEMENT_FREE
    }
    

    To use it in a template you should export a class like this this:

    export class Substitutions{
        public substitutionsType: SubstitutionType; 
    
        @Input() public set substitution(val: number) {
            console.log(value);
        };
    }
    

    Then to use it in your template you will write something like this:

    <span class="Substitutions" subsitutionType="subsitution.REPLACEMENT_FREE"></span>
    

    I used this post like an example: Pass enums in angular2 view templates