4

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?

dooglu
  • 261
  • 1
  • 4
  • 12
  • 1
    what do you mean by template? – Niladri Apr 06 '18 at 12:02
  • I mean template Angular 2, so maybe to to this: `public substitutionsType = SubstitutionType;`? – dooglu Apr 06 '18 at 12:02
  • 1
    There is already an answer for this https://stackoverflow.com/questions/35923744/pass-enums-in-angular2-view-templates – Niladri Apr 06 '18 at 12:05
  • since it's a type, replace your = by a column : – Pac0 Apr 06 '18 at 12:05
  • Possible duplicate of [Pass enums in angular2 view templates](https://stackoverflow.com/questions/35923744/pass-enums-in-angular2-view-templates) as per [Niladri's comment above](https://stackoverflow.com/questions/49692387/how-to-use-enum-in-typescript/49692472#comment86396757_49692387). – ComFreek Apr 06 '18 at 12:06
  • Here is solution: https://stackoverflow.com/a/40034109/9579547 – dooglu Apr 06 '18 at 12:24

2 Answers2

3

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

JoseJimRin
  • 372
  • 1
  • 4
  • 20
1

Currently, you declare substitutionsType to be of type SubstitutionType, i.e. it can hold one of the enumeration values. What you want to achieve, however, is to assign the (whole) enumeration to the instance variable:

class A {
    public substitutionsType = SubstitutionType;
}

// Now typechecks
(new A).substitutionsType.REPLACEMENT_FREE;

The inferred type of the instance variable is typeof SubstitutionType, which you can also annotate if you like.

ComFreek
  • 29,044
  • 18
  • 104
  • 156