AngularJS - ValueChanges 只有第一次

标签 angular

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

表单有 3 个步骤:

第一步

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

第 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 中注册用户,但如果用户再次单击第二个单选按钮,代码将尝试再次注册用户,我的控制台出现错误(电子邮件已退出..) .

解决这个问题的方法是什么?我只想在第一次触摸单选按钮时注册用户。我没有在这里使用提交,因为我有其他字段和步骤。我只想注册用户的第一步。

最佳答案

您可以使用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()

关于AngularJS - ValueChanges 只有第一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033657/

相关文章:

javascript - 我如何更改 ng-zorro 中进度圈的描边颜色?

angular - angular 的@Attribute 装饰器是如何工作的?

javascript - 将数据从两个组件传输到服务( Angular 2)

javascript - 通过窗口访问 Angular2 应用程序丢失了 "this"的范围

html - 如何将 s router-outlet 的内容居中

javascript - Angular2 防止在路由更改时破坏组件

angular - 如何在Angular4中的同一 View 中使用ngx分页实现多个分页?

javascript - Angular2 语法错误 : expected expression, 得到 '<'

angular - 如何在 typescript 中编辑CSS样式?

javascript - Promise 在 setTimeOut 之后没有得到解决