Search code examples
angularangular-reactive-formsangular-pipe

Angular Pipe not working when selecting the browser suggestion for input field


I am using the built-in pipe titlecase on an input field - username in a reactive form. It is working properly only when i am typing in the input field and its not working when i select from the browser suggestion for that input field, even when i focused out.

app.component.ts

ngOnInit() {
    this.signupForm = new FormGroup({
      'userData': new FormGroup({
        'username': new FormControl(null, [Validators.required, this.forbiddenNames.bind(this)]),
        'email': new FormControl('abc@test.com', [Validators.required, Validators.email], this.forbiddenEmails)
      }),
      'gender': new FormControl('male'),
      'hobbies': new FormArray([])
    });
}

app.component.html

<form [formGroup]="signupForm" (ngSubmit)="onSubmit()">
    <div formGroupName="userData">
        <div class="form-group">
           <label for="username">Username</label>
             <input
                  type="text"
                  id="username"
                  formControlName="username"
                  class="form-control"
                  [value]="signupForm.get('userData.username').value | titlecase">
                  <span *ngIf="signupForm.get('userData.username').errors['required']">
                      This field is required
                  </span>
          </div>
         ...
     </div>
     <button class="btn btn-primary" type="submit">Submit</button>
 </form>

When i am typing it is working fine

enter image description here

When i am selecting from the browser selection it is not working
Though i focused out of the input field its still in uppercase.

enter image description here Can someone help on what i am doing wrong.


@Haifeng Zhang This is the scenario i mentioned in the question, i replicated in your stackblitz demo

enter image description here


Solution

  • 2nd edit: enter image description here Check the very last action in the GIF, I type in space and ADD transforms to Add

    That's how titlecase work, the definition is:

    Transforms text to title case. Capitalizes the first letter of each word, and transforms the rest of the word to lower case. Words are delimited by any whitespace character, such as a space, tab, or line-feed character.

    1st edit:

    I use tempref and it works for me: Demo is here

    enter image description here

    app.component.html:

    <form [formGroup]="signUpForm">
      <div formGroupName="userData">
        <label for="username">Username</label>
        <input type="text" formControlName="username" #username [value]="username.value | titlecase">
      </div>
      <pre>{{ signUpForm.value | json }}</pre>
    </form>
    

    app.component.ts:

     constructor(public fb: FormBuilder) {
        this.signUpForm = this.fb.group({
          userData: this.fb.group({
            username: '',
            email: ''
          })
        });
      }
    

    It also works when I copy a uppercase 'ABC' into the input field

    enter image description here enter image description here enter image description here