How to work with select and options at angular 2

Asked

Viewed 2,038 times

0

have 2 fields select,precise q when I select a value automatically it select an array of options for the other

Home Component

  eventAt:string=''
  @Output() selecionaEvento =  new EventEmitter<String>()
  @Output() eventos:string[]=[
  'ForumCientifico',
  'RoadSec',
  'MundoSenai'
   ]
    ofForum:string[]=[
   'IOTOficina',
   'BigData',
   'CoffeBreak'
   ]
    ofRoad:string[]=[
   'Drrone',
   'LOCKPICK'
    ]

   ofSenai:string[]=[
   'Palestra sobre internet das coisas',
    'LabAberto',
'IniciaçaoCientifica'
 ]

  ofAtual:string[]=[]
  eventoSelecionado(evento: String){

if(evento === "ForumCientifico") return this.ofAtual=this.ofForum
else if(evento === "RoadSec") return this.ofAtual=this.ofRoad
else if(evento === "MundoSenai") return this.ofAtual=this.ofSenai

Home HTML

<div *ngIf="eventos.length <= 0">
            <p>Não Foram Encontrados Eventos</p>
          </div>
          <div *ngIf="eventos.length > 0">
            <label class="col-md-4 control-label" for="evento">Selecione o evento</label>
            <div class="col-md-6">
              <select id="evento" name="evento" class="form-control" [(ngmodel)]="ofAtual">
                <option *ngFor="let evento of eventos" [value]="evento">{{evento}}</option>
              </select>
            </div>
            </div>
          </div>
      </div>
      <div class="col-md-6">
        <div *ngIf="eventos.length <= 0 || ofAtual.length <= 0">
          <p>Não Temos Oficinas Disponiveis Para Este Evento.</p>
        </div>
        <div class="form-group">
            <label class="col-md-4 control-label" for="oficina">Selecione a oficína</label>
            <div class="col-md-6">
            <select id="oficina" name="oficina" class="form-control">
              <option *ngFor="let oficina of ofAtual">{{oficina}}</option>
            </select>
            </div>
            </div>

      </div>

I want that when selecting an event it check the name and select by string

1 answer

0


No Two-way databind

<select (change)="onChange($event.target.value)">
      <option *ngFor="let i of values">{{i}}</option>
 </select>

onChange(eventValue) {
    console.log(eventValue);
}

With Two-way databind

<select [ngModel]="selectedValue" (ngModelChange)="onChange($event)" name="sel2">
    <option [value]="i" *ngFor="let i of values">{{i}}</option>
</select>
export class AppComponent {
  values= 'one two three'.split(' ');
  selectedValue = 'two';
  onChange(newValue) {
    console.log(newValue);
    this.selectedValue = newValue;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.