nsr nsr - 2 months ago 14
AngularJS Question

working with select options and an object in angularjs

I need to display category value in a dropdown using select options from this object:

[{
"_id": "57b4508923a10cd83c79a301",
"created_by": "1",
"category": "Criminal law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471434889599",
"created_date": "1471434889599"
}, {
"_id": "57b466b34f94fc982a0d926d",
"created_by": "1",
"category": "Business Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1471440563159",
"created_date": "1471440563158"
}, {
"_id": "57c6be3ae74cdc1d6a9b2224",
"created_by": "1",
"category": "Corporate Law",
"__v": 0,
"delete_status": "0",
"active_status": "1",
"modified_date": "1472642618768",
"created_date": "1472642618768"
}]


I used below code but got undefined:

<select ng-model="category" ng-options="users.id as users.category for item in items"></select>


Can any one help me?

Answer

Try this

var app = angular.module('app', []).controller('countryCtrl', ['$scope',
  function($scope) {
    $scope.records = [{
      "_id": "57b4508923a10cd83c79a301",
      "created_by": "1",
      "category": "Criminal law",
      "__v": 0,
      "delete_status": "0",
      "active_status": "1",
      "modified_date": "1471434889599",
      "created_date": "1471434889599"
    }, {
      "_id": "57b466b34f94fc982a0d926d",
      "created_by": "1",
      "category": "Business Law",
      "__v": 0,
      "delete_status": "0",
      "active_status": "1",
      "modified_date": "1471440563159",
      "created_date": "1471440563158"
    }, {
      "_id": "57c6be3ae74cdc1d6a9b2224",
      "created_by": "1",
      "category": "Corporate Law",
      "__v": 0,
      "delete_status": "0",
      "active_status": "1",
      "modified_date": "1472642618768",
      "created_date": "1472642618768"
    }];
  }
])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="countryCtrl">
  <select ng-init="text._id = records[0]._id" ng-model="text" ng-options="record.category for record in records track by record._id">
  </select>
</div>