0

I'm trying to create a dropdown list from an angular controller: My html code:

  <select>
  <option value="" disabled selected >Choose your option</option>
  <option ng-repeat="ty in type">{{ty.$value}}</option>
  </select>

Controller code:

var typeRef = new Firebase(FIREBASE_URL + "/type");
var typeArray = $firebase(typeRef).$asArray();
typeArray.$loaded().then( function (data) {
            $scope.type = data;
            console.log(data);
        });

In this case it works:

<ul id='dropdown2' class='dropdown-content'>
<li ng-repeat="ty in type"><a href="#!">{{ty.$value}}</a></li>
</ul>

The ty.$value works in above code, but in this <select> tag doesn't show anything any idea why is that?

11
  • post your controller code Commented Aug 18, 2015 at 17:42
  • added in the question Commented Aug 18, 2015 at 17:43
  • You should not nest option tags inside option! so use ng-repeat in the select tag! Commented Aug 18, 2015 at 17:45
  • @Bellash I want to have multiple choices in one dropdown, if I do what you said I will have multiples dropdowns Commented Aug 18, 2015 at 17:46
  • You don't need $loaded(): $scope.type = $firebase(typeRef).$asArray(). And please don't use console.log to debug the AngularFire loading sequence. Just put <pre>{{ type | json }}</pre> in your HTML. The docs are great too: firebase.com/docs/web/libraries/angular/guide/… (especially if you upgrade to the latest version). Commented Aug 18, 2015 at 19:31

2 Answers 2

3

You don't need to use ng-repeat, you can use ng-options

<select ng-options="ty.$value for ty in type" ng-model="something">
<select>
Sign up to request clarification or add additional context in comments.

5 Comments

<select ng-options="ty.$value for ty in type" ng-model="achvtype"> <option value="" disabled selected >Choose your option</option> </select> this code won't work, no errors in console, and the type array i correct
@AndreiMaieras, can you post a broken jsbin? Here is a working jsbin: jsbin.com/povaqimuca/edit?html,js,output
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css" rel="stylesheet" type="text/css" /> add this css to yours and you have the broken jsbin
I use materialize.css for styling and for some reason, it stops the listing of the elements brought in from the controller
jsfiddle.net/tr2uL76L/1 you can see here, if you remove the browser-default class the dropdown will not show the data
1

Use

<select ng-options="ty.$value as ty.$value for ty in type" ng-model="achvtype"> <option value="" disabled selected >Choose your option</option> </select>

http://jsbin.com/juhamul/edit?html,js,output

4 Comments

This looks great, but from some reason still no item in my dropdown, I'm lost.
How is possible that my object printed in the console looks exactly the same with yours, but no item in the dropdown, can a HTML element interfere in this??
can you make changes in jsbin? also give a sample array
0: Object $$hashKey: "object:28" $id: "Finance" $priority: null $value: "finance" __proto__: Object This is 1 element from the array, I'll try to change the jsbin, thanks a lot

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.