angular - 调用 setValue 时未触发 FormControl ValueChanges 事件

标签 angular

我有以下代码用于 Angular 4 type-ahead 组件:
FormGroup 中有一个与 html 绑定(bind)的 FormControl,它工作得很好。

this.ProfileForm.controls["code"]

当我在文本框中更改时,会触发 valueChanges 事件。现在,当我通过编程方式更新 formcontrol 值时,不会触发 valueChanges 事件。
以下是我编写的代码行。
this.ProfileForm.controls["code"].setValue("someValue");

this.ProfileForm.controls["code"].valueChanges.subscribe(() => {
            console.log("modified");}, () => { console.log("error") }, () => { console.log("completed") });

任何建议表示赞赏。

最佳答案

将其更改为先订阅,然后设置值

this.ProfileForm.controls["code"].valueChanges.subscribe(() => {
            console.log("modified");}, () => { console.log("error") }, () => { console.log("completed") });

this.ProfileForm.controls["code"].setValue("someValue");

因为在您的代码中,您正在更改值(value),而您尚未订阅。

关于angular - 调用 setValue 时未触发 FormControl ValueChanges 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45960672/

相关文章:

angular - 如何在Angular中注册触摸移动事件?

Angularfire2 导航后数据不可用

javascript - 将 api 返回的位图转换为 angular2 中的图像

javascript - 如何在 typescript 中从 Map 获取所有键?

angular - Angular Intercept catcherror还捕获除404之外的其他错误,并阻止了应用程序按原样工作

javascript - 如何使用 ng2-dragula 拖放到多个 Angular2 组件

html - Angular material 2 table - 使用 TemplateRef 和 ngTemplateOutlet 定义列

javascript - 类似于 Cron 作业的刷新 Angular 组件

angular - 版本升级到 Ionic 4 时找不到模块 'ionic-angular' .ts

javascript - Angular2 - 具有单独模板/样式文件的库组件