问题
I am trying to get options in select button but its not showing up.
my code is as below:
index.html:
<select ng-model="main.order" ng-options="order as order.title for order in main.orders"></select>
app.js:
$http.get('src/json/sortingKeys.json').success(function(response){
vm.orders = response.orders;
vm.order = vm.orders[0];
});
Before it was working fine and i was getting values in select options. but then I wanted to use material design from http://materializecss.com/about.html. I have also initialized "select" as per what they told :
$(document).ready(function(){ $('select').material_select();});
Eventhough I am getting values to html page, I am not getting them inside select options. please refer the below code which i can see after inspecting in browser:
<div class="select-wrapper"><input class="select-dropdown" readonly="true"
data-activates="select-options-082235b8-d9be-505d-90ab-c4cc9b9bb765" value=""
type="text"><i class="mdi-navigation-arrow-drop-down"></i>
<select class="selectOptions ng-pristine ng-untouched ng-valid initialized" ng-model="main.order"
ng-options="order as order.title for order in main.orders">
<option selected="selected" label="Year Ascending" value="object:16">Year Ascending</option>
<option label="Year Descending" value="object:17">Year Descending</option>
<option label="Title Ascending" value="object:18">Title Ascending</option>
<option label="Title Ascending" value="object:19">Title Ascending</option>
</select>
</div>
Please let me know what am I doing wrong. Code is available at github: https://github.com/pradeepb6/firstExample/tree/master/app
回答1:
I explained how to fix it in this issue.
Basically you need to create a directive for all select elements, it can be bound to the select element, and call $(element).material_select();
in its link function.
Also, since it won't know when the data that came from the server is done, you'll need to prevent the select from being created at all. a ngIf will do the trick. So your select will look like this:
<select
ng-model="main.order"
ng-options="order as order.title for order in main.orders"
ng-if="main.orders">
</select>
As for the directive, you can use this one. I use this in my code that will soon hit production ;)
回答2:
This happens to always work when you 'dropdown' (pun intended) to use the default browser approach to the select instead of materializing it:
<select class="browser-default" ng-model="student.name">
<option ng-repeat="item in students" value="{{ item.$id }}"> {{ item.name }} </option>
</select>
The take away is the class="browser-default"
part. See here: http://materializecss.com/forms.html
The styling doesn't look like material, however, you could drop in a couple of lines of CSS to style it up like material, without suffering the missing dynamic rendering capabilities.
来源:https://stackoverflow.com/questions/29402495/values-not-showing-up-in-select-button