gpt4 book ai didi

angular - 如何在angular2中使用FormControl statusChanges?

转载 作者:太空狗 更新时间:2023-10-29 17:18:56 25 4
gpt4 key购买 nike

我是 angular2 的新手,我想在用户在下拉列表中选择某个值时触发一个函数。所以我尝试实现 FormControl 类的 statusChange 但它没有被触发,

wonder how and when to use the statusChange in angular2this is my code

class policy{
subCategory: FormControl = new FormControl();
ngOnInit() {
this.subCategory.statusChanges.subscribe(
s => {
alert('success');
}, e => {
alert('error');
}, () => {
alert('complete');
}
);
}
}

我认为通过实现 statusChanges 我可以在下拉列表中的每次值更改时触发成功函数,显然它现在可以工作了。

更新 1

我已经更新了 plunkr

最佳答案

如评论所述,您可能需要 valueChanges。但是,对于通过搜索 statusChanges 的工作原理来到这里的成千上万的其他人来说,这里是:

this.subCategory.statusChanges.subscribe( (status) => {
console.log(status); //status will be "VALID", "INVALID", "PENDING" or "DISABLED"
})

The docs描述这 4 个可能的值如下:

  • VALID: This control has passed all validation checks.
  • INVALID: This control has failed at least one validation check.
  • PENDING: This control is in the midst of conducting a validation check.
  • DISABLED:This control is exempt from validation checks. These status values are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled.

关于angular - 如何在angular2中使用FormControl statusChanges?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com