最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the value of a select option in Angular - Stack Overflow

programmeradmin3浏览0评论

i have this user.model.ts, Ihave a list of users that I can edit by clicking on a button that filters the user's data and puts it in a modal of bootstrap, with the [ngModel] in select tag i can get the country of my user but when i change the country and submit the form I receive the current country but not its id, it should be noted that when I use [value] in the option it does not show me the current country, how can i get the value and not the name? Thanks.

in userponent.ts

updateUser(formActualizarTeam) {
  console.log(this.user);
}

in user.model.ts

export class UserModel{
 name: string;
 idCountry: number;
}

in userponent.html

<form
  #formUpdate="ngForm"
  (ngSubmit)="updateUser(formUpdate)">

<select
   id="country"
   name="idCountry"
   class="form-control"
   [(ngModel)]="user.idCountry">
   <option *ngFor="let c of countries">{{ c.name }}</option>
</select>

</form>

i have this user.model.ts, Ihave a list of users that I can edit by clicking on a button that filters the user's data and puts it in a modal of bootstrap, with the [ngModel] in select tag i can get the country of my user but when i change the country and submit the form I receive the current country but not its id, it should be noted that when I use [value] in the option it does not show me the current country, how can i get the value and not the name? Thanks.

in user.ponent.ts

updateUser(formActualizarTeam) {
  console.log(this.user);
}

in user.model.ts

export class UserModel{
 name: string;
 idCountry: number;
}

in user.ponent.html

<form
  #formUpdate="ngForm"
  (ngSubmit)="updateUser(formUpdate)">

<select
   id="country"
   name="idCountry"
   class="form-control"
   [(ngModel)]="user.idCountry">
   <option *ngFor="let c of countries">{{ c.name }}</option>
</select>

</form>

Share Improve this question asked Nov 28, 2019 at 5:40 Carlos MoralesCarlos Morales 922 silver badges7 bronze badges 1
  • can you show the countries array.,? – Ganesh Commented Nov 28, 2019 at 5:45
Add a ment  | 

4 Answers 4

Reset to default 3

You need bind [value] to idCountry , also to set default value the selected value should match some option value :

<select
   id="country"
   name="idCountry"
   class="form-control"
   [(ngModel)]="user.idCountry">
   <option *ngFor="let c of countries" [value]="c?.idCountry">{{ c?.name }}</option>
</select>

Also to load the default value there are two option:

ponent.ts

ngOnInit(){
this.user['idCountry'] =  this.countries[0]['id']; //  set this after country data is loaded
}

OR

this.user['idCountry'] = '';

ponent.html

<select
   id="country"
   name="idCountry"
   class="form-control"
   [(ngModel)]="user.idCountry">
   <option value="" [disabled]="true"> Select country</option> // set some placeholder
   <option *ngFor="let c of countries" [value]="c?.idCountry">{{ c?.name }}</option>
</select>

I think you need to use the [value] attribute to match the options to select ngModel

then the code will be (if you have idCountry in countries array)

<select id="country" name="idCountry" class="form-control [(ngModel)]="user.idCountry">
   <option *ngFor='let c of countries' [value]='c.idCountry'>{{ c.name }}</option>
</select>

You have to set [value] for your option tag like below [value]="c.idCountry"

<form
  #formUpdate="ngForm"
  (ngSubmit)="updateUser(formUpdate)">

<select
   id="country"
   name="idCountry"
   class="form-control"
   [(ngModel)]="user.idCountry">
   <option *ngFor="let c of countries" [value]="c.idCountry">{{ c.name }}</option>
</select>

</form>

you will get value now try priting it

updateUser(ngform){
    console.log(ngform.form.value)
  }

You can get selected value by using (change) event.

In your html:

<select (change)="onChange($event)"
   id="country"
   name="idCountry"
   class="form-control">
   <option *ngFor="let c of countries">{{ c.name }}</option>
</select>

In you .ts:

onChange(e: any) {
    console.log(e.target.value);
}
发布评论

评论列表(0)

  1. 暂无评论