AngularJS - ValueChanges只有第一个

时间:2017-09-04 09:01:01

标签: angular

我正在开发AngularJS PWA应用程序(在我的注册组件中有表单)

表格有3个步骤:

第1步

  • 名字
  • 姓氏
  • 电子邮件
  • 密码
  • 电话号码

第2步

  • 例如
  • 例如

第3步

  • 例如
  • 例如

当我的用户完成第一步时,我有单选按钮:

 <div class="radio">
        <label><input type="radio" name="step2" formControlName="step2" value="1"> Fortsæt</label>
        <label><input type="radio" name="step2" formControlName="step2" value="2"> Ring mig op</label>
 </div>

代码:

this.rForm.get('step2').valueChanges.subscribe(
    (step2) => {
        // here I have user registration width angularfire2
        this.afAuth.auth.createUserWithEmailAndPassword(email, password).then(function(user) {
    }
});

当用户单击单选按钮之一时,此代码将在firebase中注册用户,但如果用户再次单击第二个单选按钮,代码将再次尝试注册用户,我的控制台出现错误(电子邮件已经退出..) 。

这是什么解决方案?我想只注册用户第一次触摸单选按钮。我不是在这里提交,因为我有另外的字段和步骤。我只希望注册用户第一步。

1 个答案:

答案 0 :(得分:1)

您可以使用firsttake运算符

import 'rxjs/add/operator/first';
this.rForm.get('step2').valueChanges.first().subscribe(

this.rForm.get('step2').valueChanges.take(1).subscribe(

另见Angular 2 using RxJS - take(1) vs first()

相关问题