admin 管理员组文章数量: 1086019
I have a select menu using ng-options
to repeat the options, and I have a default <option>
inside the menu:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">Choose an option</option>
</select>
Problem: I want to make it so that the text inside that default option changes based off another property in the $scope
, and I tried the following and it doesn't work:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="" ng-if="!optional">Choose an option</option>
<option value="" ng-if="optional">Choose an option (optional)</option>
</select>
It seems it will only show one default <option>
element, so I also tried the following with ng-show
but it doesn't work either:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">Choose an option <span ng-show="optional">(optional)</span></option>
</select>
I know you can also do <option ng-repeat>
inside the select menu, but the data for the options es from an AJAX call and doesn't update correctly when the data first es in, so I'm sticking to using <select ng-options>
.
Here's a JSFiddle with the problem.
Any suggestions for how I could go about getting this to work?
I have a select menu using ng-options
to repeat the options, and I have a default <option>
inside the menu:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">Choose an option</option>
</select>
Problem: I want to make it so that the text inside that default option changes based off another property in the $scope
, and I tried the following and it doesn't work:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="" ng-if="!optional">Choose an option</option>
<option value="" ng-if="optional">Choose an option (optional)</option>
</select>
It seems it will only show one default <option>
element, so I also tried the following with ng-show
but it doesn't work either:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">Choose an option <span ng-show="optional">(optional)</span></option>
</select>
I know you can also do <option ng-repeat>
inside the select menu, but the data for the options es from an AJAX call and doesn't update correctly when the data first es in, so I'm sticking to using <select ng-options>
.
Here's a JSFiddle with the problem.
Any suggestions for how I could go about getting this to work?
Share Improve this question asked Sep 17, 2014 at 22:37 Herman TranHerman Tran 1,5912 gold badges12 silver badges19 bronze badges2 Answers
Reset to default 8Your first try didn't work because, as said by the documentation of select
in AngularJS, only "a single hard-coded <option>
element can be nested into the <select>
element".
Your second try didn't work because the <option>
element can only receive "text (with eventually escaped characters like é
)" as content, and not any tag like the <span>
you've tried.
The solution is simply to use interpolation:
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">Choose an option {{optional ? '(optional)' : ''}}</option>
</select>
Have you tried setting the text with template markup?
<select ng-model="selected" ng-options="d.id as d.value for d in data">
<option value="">{{defaultOptionText}}</option>
</select>
Working at this jsfiddle: http://jsfiddle/udu9byka/2/
本文标签: javascriptAngularJSChanging text of default option inside ngOptionsStack Overflow
版权声明:本文标题:javascript - AngularJS - Changing text of default option inside ngOptions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744033442a2521928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论