最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I set a prompt on a select using ng-options? - Stack Overflow

programmeradmin0浏览0评论

I have a select that uses ng-options to populate the select as follows:

<select class="form-control" 
   ng-model="Data.selectedPerson" 
   ng-options="v as (v.Name ) for v in Data.people track by v.Name">
</select>

I don't want to add a value to the collection for a default if the people collection is empty or no value is selected yet. I would like to have a prompt in the select to encourage them to use the select. Thanks for your suggestions.

I have a select that uses ng-options to populate the select as follows:

<select class="form-control" 
   ng-model="Data.selectedPerson" 
   ng-options="v as (v.Name ) for v in Data.people track by v.Name">
</select>

I don't want to add a value to the collection for a default if the people collection is empty or no value is selected yet. I would like to have a prompt in the select to encourage them to use the select. Thanks for your suggestions.

Share Improve this question edited Aug 29, 2014 at 19:37 PSL 124k21 gold badges256 silver badges243 bronze badges asked Aug 28, 2014 at 18:58 user3648646user3648646 7112 gold badges16 silver badges24 bronze badges 4
  • it means you want show promt when your form submited ? – Narek Mamikonyan Commented Aug 28, 2014 at 18:59
  • you want placeholder?? – harishr Commented Aug 28, 2014 at 19:02
  • I would like a Placeholder – user3648646 Commented Aug 28, 2014 at 19:22
  • @user3648646 placeholder is not supported for select, that is why there is a default option. If you really want a placeholder you may have to create some kind of mask.. – PSL Commented Aug 28, 2014 at 19:52
Add a ment  | 

1 Answer 1

Reset to default 10

Just add a default option, just so angular will use this option when there is nothing selected in the ngModel or an invalid item is populated in the model. This way you don't need to add an empty value in your collection.

<select class="form-control" 
   ng-model="Data.selectedPerson" 
   ng-options="v as v.Name for v in Data.people  track by v.Name">
   <!-- Add your default option here -->
   <option value="">Please select a person</option>

</select>

You could also change the text based on the condition:-

  <option value="">{{ Data.people.length ? "Please select a person" : "No one available for selection" }}</option>

You can also remove it from DOM if it has already a selected value.

   <option ng-if="!Data.selectedPerson" value="">Please select a person</option>
发布评论

评论列表(0)

  1. 暂无评论