如何在绑定到带有 AngularJs 的模型的选择中为选项元素设置标题属性? [英] How to set title attribute for option elements in a select bound to a model with AngularJs?

查看:15
本文介绍了如何在绑定到带有 AngularJs 的模型的选择中为选项元素设置标题属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 html 中有一个 select 元素绑定到一个 ng-model.

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

类似于这个例子:

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

我的 accountType 看起来像这样:

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 很受欢迎,但我的理想解决方案是没有(jQuery、DOM 修改)的东西.

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

推荐答案

ngOptions 目前不支持 title.相反,在选项标签上使用 ngRepeat,或者(更难)扩展 ngOptions 以支持您的需求.

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>

这篇关于如何在绑定到带有 AngularJs 的模型的选择中为选项元素设置标题属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆