0

I send generate a list in C# controller and translate it to Json and angular receive that at front end. when i try to use ng-options to loop this array and get array value, i always got index.

<select class="selectpicker" data-show-subtext="true" data-live-search="true" ng-model="groupArray" ng-options="k for (k,v) in groupArray">

enter image description here

3 Answers 3

1

Hi a simple working example should be like this:

Inside the controller

$scope.JSONdata = [{ 'name' : 'Name 1', 'sname' : 'Sname 1' }, { 'name': 'Name 2', 'sname': 'Sname 2' }, { 'name': 'Name 3', 'sname': 'Sname 3' }]

From the front end

<select ng-model='selectedName'
                ng-options='item as item.name for item in JSONdata'>            
</select>

//Retrieves the whole object. From here you can access both name and 
//sname of the selected item.
SelectedObject : {{selectedName}} 

How the ng-options is working is like this :

  1. Retrieve the whole item
  2. Display only item.name in the options (select)
  3. Whilst looping through every item in the JSONdata scope variable
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this in your ng-options?

v for (k,v) in groupArray

(I can't try at the moment and I am not sure, but it could be worth a try)

1 Comment

I tried, but i show me array size is 6, because it repeat twice. I donot know what happened.
0

As per your object structure you can simply use.

v for (k,v) in groupArray

k is key and will return index, if you want to access the property value of any given object you can try something like.

v.propertyname for (k,v) in object

where propertyname is a property in your object structure.

For more information refer ngOptions docs ngOptions

Comments

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.