0

Populating select with JSON in AngularJS with ng-options,

<select id="routeName-{{$index}}" ng-model="row.routeName" ng-options="routeName.id as routeName.name for routeName in routeNames" ng-change="flowRootChange($index)" style="width: 115px;"></select>

input JSON is below:

scope.routeNames=[{"id":"1","name":"routeName1"},{"id":"2","name":"routeName2"}];

but in html displaying like below:

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-options="routeName.id as routeName.name for routeName in routeNames" ng-model="row.routeName" id="routeName-0" class="ng-pristine ng-valid ng-touched">
   <option label="routeName1" value="string:1">routeName1</option>     
   <option label="routeName2" value="string:2">routeName2</option>
</select>

By reading the data using JQuery I'm getting string:2 as output.

console.log($("#routeName-"+rowid +" :selected").val());

I can't use ng-model/$scope.row.routeName because this drop down is in Table. How can i read selected value(without string:) without using split() method?. I'm using 'AngularJS v1.4.8' varsion.

2
  • You can also use: ng-options="routeName as routeName.name for routeName in routeNames track by routeName.id" Commented Jan 5, 2016 at 11:11
  • @SujataChanda this is populating options fine but selected value not setting, below approach is working fine one i accepted. Commented Jan 5, 2016 at 11:17

2 Answers 2

1

You can use ng-repeat instead of ng-options

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-model="row.routeName" >
   <option ng-repeat='routeName in routeNames' label="routeName{{ routeName.id  }}" value="{{ routeName.id }}">{{ routeName.name }}</option> 
</select>
Sign up to request clarification or add additional context in comments.

Comments

-2

Check this one :-

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-model="name" >

{{ routeName.name}}

1 Comment

please put detailed answer.

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.