0

necesito ayuda estoy tratando de hacer un select option dependiente, el problema es que necesito guardar el valor del id(carrera.id) del primer select en una variable en el component.ts

HTML

<td>
                            <select [disabled]="flagAsignaturasCupo" [(ngModel)]="carrera.id" class="form-control"
                                    id="carrera" name="carrera" required>
                                <option value="0" selected>SELECCIONE UNA CARRERA</option>
                                <option value="{{carrera.id}}" *ngFor="let carrera of carrerasdocente" class="small">
                                    {{ carrera.descripcion }}
                                </option>
                            </select>
                        </td>

Necesito guardar el valor de carrera.id de mi seleccion anterior en una variable y usarla en el getAsignatura para usarla en parametros y me funcione el segundo select option

Component.ts

 getAsignaturasDocente() {
        const parametros =
        '?user_id=' + this.user.id
        + '&carrera_id=' + ***variable_que_necesito***
        this.spinner.show();
        this.service.get('docentes/asignaturas' + parametros).subscribe(
            response => {
                this.asignaturasdocente = response['asignaturasdocente'];
                this.spinner.hide();
            },
            error => {
                this.spinner.hide();
            });
    }

2 respuestas 2

1

Yo usaria los eventos que trae Angular, seria algo asi

<select [disabled]="flagAsignaturasCupo" [(ngModel)]="carrera.id" class="form-control" id="carrera" name="carrera" required (change)="onCarrera($event)">
    <option value="0" selected>SELECCIONE UNA CARRERA</option>
    <option value="{{carrera.id}}" *ngFor="let carrera of carrerasdocente" class="small">{{ carrera.descripcion }}</option>
</select>

En el controller crearia la funcion onCarrera(e)

  onCarrera(e: any) {
      console.log(e.target.value);
      const idCarrera = e.target.value;
  }
1

Con ngModel es simple:

<select (change)="changeSelect(carrera.id)" [(ngModel)]="select">
    <option value="0" selected>SELECCIONE UNA CARRERA</option>
    <option value="{{carrera.id}}" *ngFor="let carrera of carrerasdocente">
        {{ carrera.descripcion }}
    </option>
</select>

En la clase component.ts:

let id:number = 0;

changeSelect(select:number){
    this.id = select;
}


getAsignaturasDocente() {
        const parametros =
        '?user_id=' + this.user.id
        + '&carrera_id=' + this.id
        this.spinner.show();
        this.service.get('docentes/asignaturas' + parametros).subscribe(
            response => {
                this.asignaturasdocente = response['asignaturasdocente'];
                this.spinner.hide();
            },
            error => {
                this.spinner.hide();
            });
}
2
  • disculpa la pregunta, por que me da el error
    – Kenny Cruz
    el 1 feb. 2020 a las 22:02
  • identificador select no esta definido, [(ngModel)]="select">
    – Kenny Cruz
    el 1 feb. 2020 a las 22:03

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.