在writeValue中更新ControlValueAccessor的输出值

时间:2018-09-09 05:43:47

标签: angular

我正在使用ControlValueAccessor创建我的第一个表单元素,并且在大多数情况下都可以使用,除了传递的初始值无效时。

我的writeValue方法将输入转换为元素期望的值,如果值与期望值不匹配,则将其保留为属性默认值。

selected: { hour: string, minutes: string, meridiem: string } = { hour: null, minutes: null, meridiem: null }

writeValue(value: string | { hour: string, minutes: string, meridiem: string }) {
    if (value !== undefined && value !== null && typeof value === "string") {
        let matches = value.trim().match(/(1?\d):?([0-5]\d)\s*([aApP][mM])/);
        if (matches) {
            this.selected = { hour: matches[1], minutes: matches[2], meridiem: matches[3].toLowerCase()};
        }
    }
    this.propagateChange(this.outputDisplay());
}

我遇到的问题propogateChange似乎没有触发?表单的初始值仍然保留,并且如果用户不更改元素,则保留无效值。我可以通过某种方式更改初始值吗?

0 个答案:

没有答案