Valuechanges事件可观察到的rxjs不会在Angular中捕获

发布于 2025-02-06 07:21:14 字数 718 浏览 1 评论 0原文

在构造函数中,我有:

this.step1FormGroup = this.formBuilder.group({
  firstLastName: new FormControl('')

});

this.step1FormGroup.get('firstLastName').valueChanges.pipe(
  map((a) => {
    console.log('oy!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    return a;
  })
);

在模板中,我有一个:

<form [formGroup]="step1FormGroup">
  <ng-template matStepLabel>Fill out your name</ng-template>
  <mat-form-field>
    <input matInput
      placeholder='Last name, First name'
      formControlName='firstLastName'>
  </mat-form-field>
  
</form>

但是我没有捕获Valuechanges,因此我没有获得console.log输出。如何在管道后使用subscribe()方法解决此问题?

In constructor I have:

this.step1FormGroup = this.formBuilder.group({
  firstLastName: new FormControl('')

});

this.step1FormGroup.get('firstLastName').valueChanges.pipe(
  map((a) => {
    console.log('oy!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    return a;
  })
);

and in template I have this:

<form [formGroup]="step1FormGroup">
  <ng-template matStepLabel>Fill out your name</ng-template>
  <mat-form-field>
    <input matInput
      placeholder='Last name, First name'
      formControlName='firstLastName'>
  </mat-form-field>
  
</form>

but I am not catching the valueChanges, so I am not getting the console.log output. How can I fix this, without using the subscribe() method after the pipe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2025-02-13 07:21:15

valueechanges函数返回可观察的,因此,如果您是pipe> pipe ing in and and and and One in and ocal>工作,在某个地方,您需要订阅该可观察的

请记住,observables默认情况下,懒惰,如果您不订阅,则不会获得数据。

this.step1FormGroup.get('firstLastName').valueChanges.pipe(
  map((a) => {
    console.log('oy!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    return a;
  })
).subscribe(); // here

另外,我建议将该逻辑从构造函数移至ngoninit,这就是所有数据初始化的地方。

The valueChanges function returns an Observable, so it's fine if you are pipeing and doing some operations in there, but in order to actually make it work, somewhere, you need to subscribe to that observable.

Remember, observables by default, are lazy, you don't get the data if you don't subscribe.

this.step1FormGroup.get('firstLastName').valueChanges.pipe(
  map((a) => {
    console.log('oy!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    return a;
  })
).subscribe(); // here

Also, I recommend moving that logic from your constructor to ngOnInit, that's where all the data initialization should be handled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文