简体   繁体   中英

Angular display data in select

I'm using angular 7 and I want when I click in update User I get a page with all info about that user so I can update what I want, but on validator attribute I don't see the value of it from database.

This is my code:

<div fxFlex="50" class="pr-1">
        <mat-form-field class="full-width">
          <mat-select placeholder="Validator *" name="validator" [formControl]="indicateurForm.controls['validator']" class="mb-1">
            <mat-option *ngFor="let v of listValidators" [value]="v" ngDefaultControl>
              {{v?.firstName}} {{v?.lastName}}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>

if I use 'input' I got the result of the validator but on 'select' I got nothing display. Thanks in advance :)

As I undestand you can't parse [value]="v" into selectBox value,

      <div fxFlex="50" class="pr-1">
        <mat-form-field class="full-width">
          <mat-select placeholder="Validator *" name="validator" [formControl]="indicateurForm.controls['validator']" class="mb-1">
            //Try to parse value like id here, example [value]="v.id" 
            <mat-option *ngFor="let v of listValidators" [value]="v" ngDefaultControl> 
              {{v?.firstName}} {{v?.lastName}}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>

Update: I found this, Its exactly what you need. https://stackoverflow.com/a/35945293/5955138

It's telling that you need to use [ngValue] property like below,

<h1>My Application</h1>
<select [(ngModel)]="selectedValue">
  <option *ngFor="let c of countries" [ngValue]="c">{{c.name}}</option>
</select>

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