Can I access to index of Object in ng-options Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Can I access to index of Object in ng-options without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I use ng-options for show data in array of object.

index.html

<select ng-model="object_choose" 
ng-options="values.id as values.label for values in array_object">
</select>
<button ng-click="get_my_array()">Get Array</button>

controller.js

app.controller('Ctrl', function ($scope) {

$scope.array_object = [
    {"id": "a", "label": "A", "my_array": [1, 2, 3, 4]},
    {"id": "b", "label": "B", "my_array": [1, 4, 8]},
    {"id": "c", "label": "C", "my_array": [2,6]}
];

$scope.get_my_array = (function() {
      console.log($scope.object_choose); //It work and show "id" a , b , c
      console.log($scope.object_choose.my_array); //It not work
  });  

});

Can I get array in Object.

Answer

The easiest way would be to store the real value in your model with:

ng-options="values as values.label for values in array_object"

You can then access this as follows:

console.log($scope.object_choose.id);       // a , b , c
console.log($scope.object_choose.my_array); // the array

The trick is to understand the many ways you can write the expression to manipulate how data a stored and displayed. The manual has all the info you need.

We are here to answer your question about Can I access to index of Object in ng-options - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji