0

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?

4
  • <select ng-model="category" ng-options="users.id as users.category for item in items"></select> Commented Sep 28, 2016 at 12:43
  • What is "category", "users" and "items" ? Which of them are you displaying as JSON above? Commented Sep 28, 2016 at 12:46
  • i want to render like this <select> <option value="57b4508923a10cd83c79a301">Criminal law</option> <option value="57b466b34f94fc982a0d926d">Business Law</option> <option value="57c6be3ae74cdc1d6a9b2224">Corporate Law</option> </select> Commented Sep 28, 2016 at 12:49
  • Possible duplicate of Working with select using Angular's ng-options Commented Sep 28, 2016 at 13:06

2 Answers 2

1

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>

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

1 Comment

sure.. but {{category}} is not displaying any thing i need id in it.. its displaying nothing after selecting
0
<select ng-model="category" ng-options="item.id as item.category for item in items"></select>

just update this in your template (y) :)

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.