我有以下代码用于 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/