2

im getting the list of object value from API and inside the object there has a property for identify the selected item i am able to bind the items list to view

here is the json data

enter image description here

here is the my code :

   <select class="form-control"  name="Underwriter">
 <option *ngFor="let underwriter of policyModel?.underwriterList" [ngValue]="underwriter.value" >{{underwriter.text}}</option>
  </select>

any way to make the selected item ?

2
  • Well what's the question?? Commented Feb 9, 2017 at 9:41
  • any way to make the selected item Commented Feb 9, 2017 at 9:46

2 Answers 2

2

You can add just a [selected] to your select and check which underwriter has property selected as true.

like so:

 <select class="form-control" (change)="onChangeunderwriter($event)" name="Underwriter">
     <option *ngFor="let underwriter of policyModel?.underwriterList" [ngValue]="underwriter.value" [selected]="underwriter.selected == true">{{underwriter.text}}</option>
 </select>

So just add:

[selected]="underwriter.selected == true"

A simplified plunker

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

4 Comments

this was work but i had add the formControlName="uw" to <select> now selected item is not working why is that ?
If you are using form, you need to set the uw with the preselected value instead.
can u plz tell me difference between [(ngModel)] and [ngModel]
basically [ngModel] is oneway binding and [(ngModel)] is two way binding :)
0

In your question you say you want to "make" the selected item, which I assume means set it. This is how I have done it, but there might be other better ways.

You need to set the [attr.selected] in the option tag

 <select class="form-control"  name="Underwriter">
 <option *ngFor="let underwriter of policyModel?.underwriterList" [ngValue]="underwriter.value"  [attr.selected]="underwriter.text === underwriterTextString ? true: null">{{underwriter.text}}</option>
  </select>

Then it your code you need to set underwriterTextString when the WebService API returns a result.

If underwriter.text does not work then try underwriter.value

However, in your comment below you now say that you want to "get" the selected item. This can be done by using the change event in the select tag

 <select class="form-control" (change)="onChangeunderwriter($event)" name="Underwriter">
     <option *ngFor="let underwriter of policyModel?.underwriterList" [ngValue]="underwriter.value"  [attr.selected]="underwriter.text === underwriterTextString ? true: null">{{underwriter.text}}</option>
      </select>

Then in your code your need something like this;

onChangeunderwriter(event) {

//the value will be set in event.target.value;


  }

1 Comment

i need to get the selected item from object list inside the object you can see there has a property call 'selected '

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.