Valuechanges事件可观察到的rxjs不会在Angular中捕获
在构造函数中,我有:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
valueechanges
函数返回可观察的
,因此,如果您是pipe> pipe
ing in and and and and One in and ocal>工作,在某个地方,您需要订阅该可观察的
。请记住,
observables
默认情况下,懒惰,如果您不订阅,则不会获得数据。另外,我建议将该逻辑从构造函数移至
ngoninit
,这就是所有数据初始化的地方。The
valueChanges
function returns anObservable
, so it's fine if you arepipe
ing and doing some operations in there, but in order to actually make it work, somewhere, you need to subscribe to thatobservable
.Remember,
observables
by default, are lazy, you don't get the data if you don't subscribe.Also, I recommend moving that logic from your constructor to
ngOnInit
, that's where all the data initialization should be handled.