简体   繁体   中英

Enums in TypeScript 2+ lost value when send from html

I have an object and enum defined like this:

export interface Character {
  name: string;
  side: Side;
}

export enum Side {
  All,
  Light,
  Dark
}

I am using this enum in my component:

import { Component, OnInit } from '@angular/core';
import { Character, Side } from '../core';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html'
})
export class TabsComponent {
  chosenList = Side.All; // initial value set to 0

  characters = [
    { name: 'Luke Skywalker', side: Side.All }, // side is set to 0
    { name: 'Darth Vader', side: Side.All } // side is set to 0
  ];

  onChoose(side: Side): void {
    this.chosenList = side; // this line goes wrong set chosenList to string 'Side.All'
  }

  getCharacters(): Character[] {
    if (this.chosenList === Side.All) {
      return this.characters.slice();
    }
    return this.characters.filter((character) => {
      return character.side === this.chosenList;
    });
  }

  onSideChosen(charInfo: Character): void {
    const pos = this.characters.findIndex((char) => {
      return char.name === charInfo.name;
    });
    this.characters[pos].side = charInfo.side;
  }
}

My html looks like this. The problem lies in (click) event. I pass enum like this to my component unfortunately my component resolve it as string.

<div id="tab-main" class="container">
  <ul class="nav nav-tabs">
    <li class="nav-item">
      <a [ngClass]="{active: chosenList === 'Side.All'}" class="nav-link" (click)="onChoose('Side.All')" style="cursor: pointer">All</a>
    </li>
    <li class="nav-item">
      <a [ngClass]="{active: chosenList === 'Side.Light'}" class="nav-link" (click)="onChoose('Side.Light')" style="cursor: pointer">Light</a>
    </li>
    <li class="nav-item">
      <a [ngClass]="{active: chosenList === 'Side.Dark'}" class="nav-link" (click)="onChoose('Side.Dark')" style="cursor: pointer">Dark</a>
    </li>
  </ul>
  <app-list [characters]="getCharacters()" (sideAssigned)="onSideChosen($event)"></app-list>
</div>

The problem is when I click on button "All button" this line of code is invoked

onChoose(side: Side): void {
    this.chosenList = side; // this line goes wrong set chosenList to string 'Side.All'
  }

however when I compare values like this

if (this.chosenList === Side.All)

it always return false because chosenList is not enum anymore but it's string.

My question is how can I pass enum from .html file to .ts I know I can resolve this problem just by sending 0 / 1 / 2 within html however this way I am loosing readability...

  1. declare field variable public Side = Side; in the component
  2. import Side from the file where it's defined
  3. change template reference to "{active: chosenList === Side.All}"

The problem is basically that in templates you don't have access to stuff defined outside the component especially if it has to be transpiled first

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM