2

I show in one select one string as an option and it works. My problem is that I would like to get the whole JSON object as the value of this option but I get the string that I am showing.

My code is the following:

    <select *ngIf="car" class="form-control" #oneDoor  (change)="getRecDet(oneDoor.value);"  required >
          <option *ngFor="let oneDoor of car.doors" [ngValue]="oneDoor">{{oneDoor.position}}</option>
    </select>

I explain it: car and object car car is the object of one car. The object is like the following:

   export class Car {
     name: String;
     doors: [{
       position: String
     }]
   }

So I am trying to show just for each door the position. That works but I would like that the value that I get from the select (not the one that I am showing, just the one that I get in my backend) would be the whole object (name, doors, position) but I am just able to get the String

I know that value={{}} gets only a string, so I was trying it with ngValue but it keeps happening the same. Any idea?

2
  • 1
    this may help you, stackoverflow.com/questions/35945001/…. {{ }} expects expression and may not recognize object Commented Dec 12, 2017 at 10:35
  • I have tried it, but I do not know how should i send that value in (change) --> <select class="form-control" [(ngModel)]="scenario" (change)="getIcos(sourceSystemID.value, scenario);"> Commented Dec 12, 2017 at 10:49

1 Answer 1

1

You should bind it to ngValue using the "box" syntax: [value].

<select  #coche (change)="getCoches(coche.value);" >
    <option type="text" *ngFor="let coche of coches" [ngValue]="coche">{{coche.nombre}}</option>                       
</select>

value is only supported for strings. If you want to bind to an object then you should use ngValue and bind to it using the "box" notation.

Sign up to request clarification or add additional context in comments.

1 Comment

I have checked it right now. I keeps returning the value of {{coche,nombre}} instead of the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.