简体   繁体   English

如何优化此角度代码?

[英]how can I optimize this angular code?

Scenario : 场景:

I have three text boxes, using material theme, If the textbox has value, I need to insert a class in the label's class so that it stays on top of the textbox (floating labels). 我有三个使用材质主题的文本框,如果该文本框具有价值,则需要在标签的类中插入一个类,以使其停留在文本框的顶部(浮动标签)。 Currently, what I did was, 目前,我所做的是

Code : 代码:

html html

    <div class="form-group form-group--float form-group--centered" [ngClass]="{'form-group--active': hasEmail}">
      <input type="email" class="form-control" formControlName="Email" (change)="emailTouched($event)">
      <label>Email Address</label>
      <i class="form-group__bar"></i>
    </div>

    <div class="form-group form-group--float form-group--centered" [ngClass]="{'form-group--active': hasPassword}">
      <input type="password" class="form-control" formControlName="Password" (change)="passwordTouched($event)">
      <label>Password</label>
      <i class="form-group__bar"></i>
    </div>

    <div class="form-group form-group--float form-group--centered" [ngClass]="{'form-group--active': hasCPassword}">
      <input type="password" class="form-control" formControlName="CPassword" (change)="cpasswordTouched($event)">
      <label>Confirm Password</label>
      <i class="form-group__bar"></i>
    </div>

ts ts

  emailTouched(ev: any) {
    if (ev.target.value !== '')
      this.hasEmail = true;
    else
      this.hasEmail = false;
  }
  passwordTouched(ev: any) {
    if (ev.target.value !== '')
      this.hasPassword = true;
    else
      this.hasPassword = false;
  }
  cpasswordTouched(ev: any) {
    if (ev.target.value !== '')
      this.hasCPassword = true;
    else
      this.hasCPassword = false;
  }

Issue : 问题 :

I hate repetitions, clearly you can see, all the three functions are doing the same thing. 我讨厌重复,很明显,您可以看到,所有三个功能都在做同一件事。 My question is, how can I make it a single function and do the same thing with different text boxes? 我的问题是,如何使它成为一个功能,并在不同的文本框中执行相同的操作?

Any advice would be helpful. 任何意见将是有益的。 Thanks. 谢谢。

First of all, you do not have to watch changes in your input elements with defining function per input. 首先,您不必通过定义每个输入的功能来监视输入元素中的更改。 In reactive forms, you can use following syntax; 在反应形式中,可以使用以下语法;

yourFormName.valueChanges.subscribe(i => {
     //do whatever you want
});

You can also take a look at this . 您也可以看看这个

Secondly, if you want to validate your form control, you can use Validators . 其次,如果要验证表单控件,可以使用Validators Just set it when you create your form control; 只要在创建表单控件时进行设置即可;

yourFormControlName = new FormControl(null, [<any>Validators.required]);

You can create your own validators, here is one tutorial . 您可以创建自己的验证器,这是一个教程

There are many ways to change class of html elements in angular. 有很多方法可以更改Angular中html元素的类。 In your case, you can do this; 就您而言,您可以执行此操作;

<div class="form-group form-group--float form-group--centered" [class.form-group--active]="CPassword.touched && CPassword.valid">
  <input type="password" class="form-control" formControlName="CPassword">
  <label>Confirm Password</label>
  <i class="form-group__bar"></i>
</div>

I recommend you to read reactive forms page. 我建议您阅读反应形式页面。 Everything you need has already written over there. 您所需的一切都已经写在那了。

Since you are already using reactive forms, why don't subscribe to the valueChanges ? 由于您已经在使用反应式表单,为什么不订阅valueChanges呢? Also, a ternary operator definitely helps. 另外,三元运算符绝对有帮助。

this.formName.controls.Email.valueChanges.subscribe(emailValue => {
    this.hasEmail = emailValue !== '' ? true: false;
});
this.formName.controls.Password.valueChanges.subscribe(password => {
    this.hasPassword = password !== '' ? true : false;
});
this.formName.controls.CPassword.valueChanges.subscribe(cPassword => {
    this.hasCPassword = cPassword !== '' ? true : false;
})

In fact, for the ternary, you can just check if the value changed is truthy or not: 实际上,对于三元组,您只需检查更改的值是否真实:

this.formName.controls.Email.valueChanges.subscribe(emailValue => {
    //if emailValue is not empty, this.hasEmail is true
    this.hasEmail = emailValue !== '';
});

Angular Reactive Forms Official Docs: https://angular.io/guide/reactive-forms Angular反应形式官方文档: https : //angular.io/guide/reactive-forms

More on Angular reactive forms: https://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html 有关Angular反应形式的更多信息: https : //blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html

Bonus: 奖金:

Looks like you want to do some validity check on the inputs. 看起来您想对输入进行一些有效性检查。 You can always write your own custom validator in your FormControl . 您始终可以在FormControl编写自己的自定义验证器。

Give a shot with Command pattern . Command模式射击。 Your code looks like a good candidate for it. 您的代码看起来像是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM