如何在angular2中使用FormControl statusChanges?

如何在angular2中使用FormControl statusChanges?,angular,Angular,我是angular2的新手,我想在用户在下拉列表中选择某个值时触发一个函数。所以我尝试实现FormControl类的statusChange,但是没有被触发 想知道如何以及何时使用angular2中的statusChange 这是我的密码 我认为通过实现statusChanges,我可以在下拉列表中的每次值更改时触发success函数,显然它现在正在工作 更新1 我已经更新了,正如评论所述,您可能希望改为valueChanges。然而,对于成千上万通过搜索statusChanges工作原理而来到

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

想知道如何以及何时使用angular2中的statusChange 这是我的密码

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

更新1


我已经更新了

,正如评论所述,您可能希望改为
valueChanges
。然而,对于成千上万通过搜索
statusChanges
工作原理而来到这里的其他人来说,这里有:

this.subCategory.statusChanges.subscribe( (status) => {
  console.log(status); //status will be "VALID", "INVALID", "PENDING" or "DISABLED"
})
如下所示描述这4个可能的值:

  • 有效:此控件已通过所有验证检查
  • 无效:此控件至少一次验证检查失败
  • 待定:此控件正在进行验证检查
  • 禁用:此控件免于验证检查。这些状态值是互斥的,因此控件不能同时为有效和无效或无效和禁用

在反应形式中,
valueChanges
statusChanges
看起来非常相似,但有两个不同的用途

状态变化
statusChanges
AbstractControl
的一个属性,每当重新计算控件的验证状态时,它都会发出一个事件。很少有状态是

价值变化
valueChanges
AbstractControl
的一个属性,每当使用UI或编程方式更改控件的值时,它都会发出一个事件

因此,
valueChanges
非常有用

提示
抽象控件
的属性意味着您可以将这些事件附加到
窗体控件
窗体数组
窗体组

例如: component.html

<form [formGroup]="form">
    <h2 class="text-center">Registration Form</h2>
    <br>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="first Name" formControlName="firstName">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="last Name" formControlName="lastName">
    </div>
    <div class="form-group">
        <input type="email" class="form-control" placeholder="email" formControlName="email">
        <span *ngIf="form.controls['email']?.errors?.required">This field is required</span>
    </div>

    <br>
    <div *ngIf="form.valid">
        Valid form
    </div>
</form>


.

如果您想了解值更改,最好订阅
valueChanges
,而不是
statusChanges
。StatusChanges是关于原始的,
有效的,
,…好的,让我检查一下,然后回来,谢谢你的帮助从StatusChanges更改为valueChanges对我没有任何帮助,但是我无法触发功能如果你能提供一个可以重现问题的插件,可能有人可以提供帮助。您的问题没有提供足够的信息来诊断问题。您的代码中甚至没有提到下拉列表。很抱歉延迟,我已经更新了plunker:我这样做了,但控制台中不断出现错误。无法读取未定义的属性“statusChanges”。我在ngAfterViewInit中有ViewChild和订阅。
<form [formGroup]="form">
    <h2 class="text-center">Registration Form</h2>
    <br>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="first Name" formControlName="firstName">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" placeholder="last Name" formControlName="lastName">
    </div>
    <div class="form-group">
        <input type="email" class="form-control" placeholder="email" formControlName="email">
        <span *ngIf="form.controls['email']?.errors?.required">This field is required</span>
    </div>

    <br>
    <div *ngIf="form.valid">
        Valid form
    </div>
</form>
ngOnInit() {
    this.form = this.fb.group({
        firstName: ['', Validators.required],
        lastName: [''],
        email: ['', Validators.required],
    });

    this.form.valueChanges.subscribe((value) => {
        console.log('valueChanges Form value ' + value);
    });

    this.form.statusChanges.subscribe((status) => {
        console.log('statusChanges Status is ' + status);
    });
}