Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
¿Cambiar menú desplegable basado en otro menú desplegable en Angular 11?

Quiero cambiar el menú desplegable en función de otro menú desplegable.

ts.archivo

 Countries: Array<any> = [ { name: '1st of the month', states: [ {name: '16th of the month'} ] }, { name: '2nd of the month', states: [ {name: '17th of the month'} ] }, { name: '3rd of the month', states: [ {name: '18th of the month'} ] }, ] states: Array<any> = []; cities: Array<any> = []; changeCountry(country: any) { this.states = this.Countries.find(cntry => cntry.name == country.target.value).states; }

archivo.html

 <mat-form-field> <mat-select [(ngModel)]="custFirst" name="custFirst" placeholder="Country" (change)="changeCountry($event)"> <mat-option *ngFor="let country of Countries" [value]="country.name" > {{ country.name }}</mat-option> </mat-select> </mat-form-field> <mat-form-field> <mat-select placeholder="State"> <mat-option *ngFor="let state of states" [value]="state.name">{{ state.name }} </mat-option> </mat-select> </mat-form-field>

Ahora mi problema es que no puedo obtener el valor en el segundo menú desplegable. Por favor, ayúdenme por qué la parte del disparador no funciona.

aquí está la imagen de los menús desplegables

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

El evento de change debe cambiarse a selectionChange . Si desea que el estado se seleccione automáticamente, agregue [(ngModel)] al menú desplegable del estado y actualícelo cuando cambie el país:

 changeCountry(country: any) { this.states = this.Countries.find( (cntry) => cntry.name == country.value ).states; this.sFirst = this.states[0].name; }
 <mat-form-field> <mat-select [(ngModel)]="custFirst" name="custFirst" placeholder="Country" (selectionChange)="changeCountry($event)"> <mat-option *ngFor="let country of Countries" [value]="country.name" > {{ country.name }}</mat-option> </mat-select> </mat-form-field> <mat-form-field> <mat-select placeholder="State" [(ngModel)]="stateFirst"> <mat-option *ngFor="let state of states" [value]="state.name">{{ state.name }} </mat-option> </mat-select> </mat-form-field>

Ejemplo de trabajo: https://stackblitz.com/edit/angular-6-stackoverflow-nzvqse?file=src/app/components/stackoverflow-solution/stackoverflow-solution.component.ts

over 4 years ago · Santiago Trujillo Relatório

0

Puede manejar eso con declaraciones de cambio de caso, pasar el valor de país a la función getStates

 country:string = 'Uruguay'; states:string[]; getStates(country: string){ switch(country) { case '': this.states = []; break; case 'República Dominicana': this.states = ['', 'Azua', 'Baoruco', 'Barahona', 'Valverde']; break; case 'Uruguay': this.states = ['', 'Artigas', 'Canelones', 'Tacuarembó']; break; case 'Venezuela': this.states = ['', 'Amazonas', 'Apure', 'Yaracuy']; break; } } this.getCountryData(this.country);

entonces a la vista:

 <mat-option *ngFor="let state of states" [value]="state.name">{{ state.name }}

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda