通过@Input传递枚举类名

时间:2019-08-26 00:07:11

标签: angular input enums

我不知道angular是否可行,但是我想在我的组件中传递类类型的参数。 目的是用我的枚举构造一个选择。但是枚举不同于所有父母。

    public class HomeType{
        HOUSE, BUILDING
    }

    public class RoomType {
        KITCHEN, BEDROOM
    }

    @Component({
        selector: 'children',
        templateUrl: './chlidren.component.html'
    })
    export class childrenComponent {

       _enum: any;

       @Input('class')
       set class (clazz: any) {
           this._enum = Object.keys(clazz)
                          .map(key => ({ value:definition[key], title: key }))
           // clazz should be of type EnumType
       }
    }


// in parent html

    ...
    <children [class]="HomeType"></children>
    ...
    <children [class]="RoomType"></children>

如果您打算做类似的事情,请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

好吧,我不知道你为什么要这么做,但是如果我正确理解你的话,可能的做法就是这样

import { Component } from '@angular/core';
export enum HomeType {
  HOUSE = "HOUSE",
  BUILDING = "BUILDING"
}

export enum RoomType {
  KITCHEN = "KITCHEN",
  BEDROOM = "BEDROOM"
}
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = 'Angular';
  test: RoomType | HomeType = HomeType.HOUSE;

  ngOnInit() {
    this.name = this.test == RoomType.KITCHEN ? 'Kitcken' : 'Not a Kitchen :)'
  }
}

PS。但是,正如您看到两个枚举中的键是否相同一样,您将遇到问题

https://stackblitz.com/edit/angular-bywsre