Angular Form Builder-根据选定的选择值属性设置值

时间:2019-10-11 11:42:49

标签: angular typescript angular-reactive-forms formbuilder

这是我的Angular Form Builder初始化小组:

    contactReason: this.formBuilder.group({
        description: '',
        source: this.sourceType()
    })

我有一个带有用于“说明”的预定义值的选择,这些值可以是许多不同的值,例如“请求信息”这是我的应用程序中其他地方使用的映射示例:

public ContactReason = {
    "request for information": 'incoming',
    "other incoming": 'incoming',
    "call update": 'outgoing',
    "information provided": 'outgoing',
    "attempted contact": 'outgoing',
    "regular contact": 'outgoing',
    "other outgoing": 'outgoing',
};

如您所见,每个描述都分配给“传入或传出”。

我想创建一种执行此映射的方法,以便当他们选择描述时,它将在此处映射“传入或传出” ...

source: this.sourceType()

private sourceType() {
//logic for source mapping based on description.value
}

任何有关上述方法的帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

通过使用映射值数组,以询问问题的方式将其映射。相当简单,但将留作参考。

public logEntryContactReason = {
        "request for information": 'incoming',
        "other incoming": 'incoming',
        "case update": 'outgoing',
        "information provided": 'outgoing',
        "attempted contact": 'outgoing',
        "regular contact": 'outgoing',
        "other outgoing": 'outgoing',
    };

private sourceType() {
    const data = this.contactForm.value;
   return this.logEntryContactReason[data.contactReason.description]
}