2

I have an object as below. I have to display this as a drop-down. I tried this but not working

HTML

    <div ng-app="app">
  <div ng-controller="MyCntrl">
    <ul class="unstyled">
      <li class="dropdown" ng-repeat="p in people">
       {{Slected value}}   <a href="#" class="dropdown-toggle" data-toggle="dropdown"><b class="caret"></b></a>
        <ul class="dropdown-menu">
          <li>{{p.color}}</li>
        </ul>
      </li>
    </ul>
  </div>
</div>

JS

var app = angular.module('app', []);
app.controller('MyCntrl', ['$scope', function($scope) {

  $scope.people = [{
    id: 1,
    color: 'blue',
    gender: 'male'
  }, {
    id: 2,
    color: 'red',
    gender: 'female'
  }, {
    id: 3,
    color: 'red',
    gender: 'male'
  }];
}]);

Fiddle : http://jsfiddle.net/ywkLt/210/

Dropdown is not showing up. I am new to this can anyone help me.

Edit:

I want to display selected value and followed by caret icon for dropdown. Like this : http://jsfiddle.net/qWzTb/3577/

But here I need caret icon. When I click on that, it has to display options.

4
  • You do not want to use <select></select>? Commented May 20, 2016 at 7:27
  • I tried that too but doesn't work for me. Commented May 20, 2016 at 7:29
  • what is this? {{Slected value}} Commented May 20, 2016 at 7:29
  • Value which is selected from dropdown @SSH Commented May 20, 2016 at 7:29

5 Answers 5

1

Your setup is wrong somewhere. I did try several references and the console errors kept popping up specifically on the bootstrap css.

<div ng-controller="MyCtrl">
  Hello, {{name}}!
  <div class="dropdown">
      <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
      <span class="caret"></span></button>
      <ul class="dropdown-menu">
        <li ng-repeat="p in people">{{p.color}}</li>
      </ul>
    </div>
</div>

Here is a working fiddle.. http://jsfiddle.net/cec58kkb/

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

Comments

1

Either you didn't have a correct linking to angular.min.js or fiddle was not responding :

I created a working plunker : https://plnkr.co/edit/9PDpoZN72xeIXCZteeJz?p=preview Code:

<select ng-model="selectedPeople" ng-options="type.color for type in people">
    <option value="">All People</option>
</select>

UPDATE:

This fiddle contains both options to create a dropdown , using your html and using select :

http://jsfiddle.net/br8bur7o/1/

2 Comments

Thanks for reply. But I want to show only caret icon for selecting options and selected value should to be display next to that.
See the Update to the answer
0

Do not think the angular is setup correctly because if I put $scope.testVal=1 in to the controller and place {{testVal}} on the HTML it is displayed as {{testVal}}

Comments

0

try this.

var app = angular.module('app', []);
app.controller('MyCntrl', ['$scope', function($scope) {

  $scope.people = [{
    id: 1,
    color: 'blue',
    gender: 'male'
  }, {
    id: 2,
    color: 'red',
    gender: 'female'
  }, {
    id: 3,
    color: 'red',
    gender: 'male'
  }];
}]);
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"/>
<div ng-app="app">
  <div ng-controller="MyCntrl">
   <select ng-model="select" ng-options="value.id as value.color for (key,value) in people "></select>
  </div>
</div>

Comments

0

Check this out. Its working now.

<select>
  <option value = "0" label = "Please Select"></option>
  <option ng-repeat="p in people" value="{{p.color}}">
    {{p.color}}
  </option>
</select>

Link here

1 Comment

Your link gives 404.

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.