troubleshooting Question

Angular 11 set dropdown to initial blank value

Avatar of Steve Hougom
Steve HougomFlag for United States of America asked on
HTMLAngular* typescript programming
2 Comments1 Solution17 ViewsLast Modified:
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>



 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();
  }


  }



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({

ASKER CERTIFIED SOLUTION
leakim971
Multitechnician
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros