Angular 6 how to leave default field in select?

0

I'm trying to leave one of the option elements as selected, but I can not put it to work on Angular 6.

app.component.html:

<select [(ngModel)]="marcaSelecionada" class="form-control" name="marca" required>
  <option value="">Selecione uma marca...</option>
  <option *ngFor="let marca of marcas" [ngValue]="marca.id">{{ marca.nome_marca | uppercase }}</option>
</select>

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { AtualizaFormularioService } from './atualiza-formulario.service';

interface MarcaModelo {
  id?: string;
  nome_marca?: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  public marcas: MarcaModelo[];
  public campoMarca: MarcaModelo = {
    nome_marca: ''
  }
  public marcaSelecionada: string = '2';

}
    
asked by anonymous 18.09.2018 / 15:33

1 answer

0

Try to put the value attribute of the select tag with the value you want it to load selected:

<select [(ngModel)]="marcaSelecionada" class="form-control" name="marca" value="marcaSelecionada" required>
  <option value="">Selecione uma marca...</option>
  <option *ngFor="let marca of marcas" [ngValue]="marca.id">{{ marca.nome_marca | uppercase }}</option>
</select>

- Related question.

- Here's an example

    
18.09.2018 / 18:54