Link to home
Create AccountLog in
Avatar of Steve Hougom
Steve HougomFlag for United States of America

asked on

Angular 11 set dropdown to initial blank value

Im trying to clear a dropdown in angular which is using reactive forms.

Basically when the user changes the Phone 2 control which is a textbox and that value isnt satisfactory(less than 9 numeric chars) I am wanting to set the dropdown Phone2 Type to a blank value.  And disable it.



User generated image

User generated image
As you can see above i can disable Phone2 Type but not clear it to the first option in the list which is blank.

The dropdown is loaded with API data.  The html markup looks like this.
   <input id="phone2" type='text' mask="(000) 000-0000" [showMaskTyped]="true" class='form-control' formControlName='phone2' (keydown)='phoneChecker($event)'/>


<select name='phoneType2' class='form-control' formControlName='phoneType2' id="phoneType2" >
   <option [value]=""></option>
    <option *ngFor="let phoneType of phoneTypesOther" 
      [value]="phoneType.entityValue"> {{phoneType.entityValue}}
    </option>
 </select>



Open in new window

 Typescript:
 phoneChecker(event){
    //console.log(event.target.value);


    console.log(event.keyCode);
  //if its a valid numeric
  if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)){

    this.phoneLength = this.form.get('phone2').value;

    if(this.phoneLength.length==9){
      this.form.get('phoneType2').enable();
    }else{
      this.form.get('phoneType2').setValue('');
      this.phoneType2 = "";
      this.form.get('phoneType2').disable();
    }

  }else{
    this.form.get('phoneType2').disable();
  }


  }



Open in new window

As you can see in the typescript method above on keypress when the characters are numeric and the length is < 9 I attempt to setValue to blank and disable the control.  I cannot set the index value to the first option in the list which is blank.  The setValue("") doesnt do it.  Ive tried reset() no luck there either.  The reactive form control code in typescript is below:
  this.form = this.fb.group({
 phoneType2: [this.phoneType2,RxwebValidators.required({conditionalExpression:(x)=>x.phone2!=''})],


 phoneType2: [this.phoneType2,RxwebValidators.required({conditionalExpression:(x)=>x.phone2!=''})],
  this.form = this.fb.group({

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Steve Hougom

ASKER

Your the best worked beautifully!!  Thanks you so much!