简体   繁体   中英

How can I bind object id from *ngFor item in Angular Select? At the moment, the select bind the same value as interpolation do.

I have a list listSiteTriage. I would like to bind to the controller selected value.

  <select [(ngModel)]="selectedSite" (ngModelChange)="onChange()">
                  <option *ngFor="let site of listSiteTriage" >{{site.Intitule}}</option>
                </select>

controller:

import { Component, OnInit, OnChanges, NgModule} from '@angular/core';

@Component({
 selector: 'xxx',
 templateUrl: './xxx.component.html',
 styleUrls: ['./xxx.component.scss']
 })
 export class Travaux implements OnInit , OnChanges {

 listSiteTriage: any[];
 site: any;


 } 
  onChange(){
  console.log(this.selectedSite);
  }
 }

Remove [ngValue]

<select [(ngModel)]="site" (ngModelChange)="onChange()">
         <option *ngFor="let site of listSiteTriage" >{{site.Intitule}}</option>
  </select>

You are iterator and the ngModel variables are both named as site rename it to be different (for instance selectedSite )

<select [(ngModel)]="selectedSite" (ngModelChange)="onChange()">
      <option *ngFor="let site of listSiteTriage" [ngValue]="site.Id">{{site.Intitule}}</option>
</select>

import { Component, OnInit, OnChanges, NgModule} from '@angular/core';

@Component({
 selector: 'xxx',
 templateUrl: './xxx.component.html',
 styleUrls: ['./xxx.component.scss']
 })
 export class Travaux implements OnInit , OnChanges {

listSiteTriage: any[];
selectedSite: any = 1;

onChange(){
      console.log(this.site);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM