1

I would like to choose which option is selected in a combobox and I want to be able to change the selected option in my Typescript.

I have only two option : yes or no. I want choose which one is selected when my combobox appears

EDIT :

It's an answer to a question from an user and i want show this combobox to allow the user to edit his answer

HTML :

<select class="form-control custom-select" name="uIsAdmin" id="uIsAdmin">
    <option *ngFor="let useradmin of userIsAdmin">{{useradmin}}</option>
</select>

TypeScript:

ngOnInit() {
    this.userIsAdmin=[
      "yes", "no"
    ];
  }
2
  • Do you want to set a default value? If so, you can achieve this by <option *ngFor="let useradmin of userIsAdmin" [selected]="option === 'yes'">{{useradmin}}</option> Commented May 2, 2019 at 15:19
  • i want to able to change the set value. Commented May 2, 2019 at 15:24

1 Answer 1

1

To set a default value you can set the selected property

<option *ngFor="let useradmin of userIsAdmin" [selected]="useradmin === 'yes'">{{useradmin}}</option>

To change the selected value programatically, you can use ngModel

<select [(ngModel)]="myChoice" class="form-control custom-select" name="uIsAdmin" id="uIsAdmin">
    <option *ngFor="let useradmin of userIsAdmin">{{useradmin}}</option>
</select>

TS:

const myChoice = "yes";

Nevertheless - with your code you provide a multiple choice for a use case that should probably only allow either yes or no. In this case you should use a radio button group.

Sign up to request clarification or add additional context in comments.

1 Comment

so, with : [(ngModel)]="yes" the option selected will be yes ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.