How to set title attribute for option elements in a select bound to a model with AngularJs?

你说的曾经没有我的故事 提交于 2019-12-08 07:27:25

问题


I have a select element in my html bound to an ng-model.

Something like this example:

<select data-ng-model="accountType" data-ng-options="at.name for at in accountTypes">  </select>

My accountTypes would look like this:

accountTypes:[
   { name:'Individual', value: 1, title: 'Individual Account'},  
   { name: 'Joint', value: 2, title: 'Joint Account'}
];

How can I achieve the final markup to look like this once the model is bound:

<option value="1" title="Individual Account">Individual</option>
<option value="2" title"Joint Account">Joint</option>

PS: jQuery hacks appreciated but my ideal solution would something (jQuery, DOM-modification)less.


回答1:


ngOptions doesn't currently support title. Instead, use a ngRepeat on an options tag, or (harder) extend ngOptions to support your needs.

<select ng-model="accountType">
    <option ng-repeat="at in accountTypes" value="{{ at.value }}" title="{{ at.title }}"></option>
</select>


来源:https://stackoverflow.com/questions/21208976/how-to-set-title-attribute-for-option-elements-in-a-select-bound-to-a-model-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!