<select> with Angular

Asked

Viewed 20 times

-1

Good evening, I’m having a doubt that I’m having trouble solving...

I’m trying to retrieve an object from one and the variable always back Undefined

Follows code snippet:

HTML

<div class="modal fade add-treatment" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
      aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Adicionar Tratamento</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <form>
              <select class="form-control" (ngModelChange)="selectTreatment($event)" name="selectTreatment" (ngModel)="selectTreatment" style="margin-top:25px;height: 34px;">
                <option [ngValue]="treatment" *ngFor="let treatment of treatments">{{treatment.name}}</option>
              </select>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button style="margin-right:15.5px" type="button" id="botaoAdicionarProduto" class="btn btn-success"
              (click)="addTreatment()">Adicionar</button>
          </div>
        </div>
      </div>
    </div>

TS file.


private treatment: Treatment = new Treatment();


addTreatment(): void {

    console.log(this.treatment)

    this.customerService.addTreatment(this.customer, this.treatment).subscribe(response => {
      let res: Response = <Response>response;
      if (res.codigo == 1) {
        alert(res.mensagem);
        this.customer = new Customer();
      }
      else {
        alert(res.mensagem);
      }
    },
      (erro) => {
        alert(erro);
      });
  }


  selectTreatment(selctedTreatment: Treatment): void{
  this.treatment = selctedTreatment;
  console.log("Object: " + this.treatment.name);
  }

1 answer

0

Opa, I found the answer in another post, I will leave here the link for the next use.

Resolution

Browser other questions tagged

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