Avatar of Steve Hougom
Steve Hougom
Flag 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.






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


HTMLAngular* typescript programming

Avatar of undefined
Last Comment
Steve Hougom

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Hougom

ASKER
Your the best worked beautifully!!  Thanks you so much!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy