javascript - angularjs select ngoptions - 以不同的格式显示所选值

标签 javascript angularjs html

我有一个国家/地区列表(带有代码和名称),当展开列表时,我想显示“代码+名称”(例如“+44 英国”),但是当我们选择一个国家/地区时,仅显示代码(“+44”)。可以用 Angular ngOptions 来做吗?

<select ng-model="userData.phoneCode" ng-options="(c.countryCallingCode.replace('00', '+') + ' ' + c.countryName) for c in countries track by c.countryId">
        <option value="" disabled selected data-i18n="Country"></option>
</select>

Controller 中国家/地区列表的格式:

countries = [
    {
      "countryCallingCode":"0033",
      "countryCode":"FR",
      "countryId":123,
      "countryName":"France"
    },
]

我想要这个:

enter image description here

我一直在谷歌中搜索,并尝试为此创建一个指令(我的 angularjs 水平为中等),但我没有找到解决方案,始终将所选选项显示为“代码+名称”。

有什么想法吗?

最佳答案

我总是在 ng-options 中使用别名“as”:

<select ng-model="userData.phoneCode" ng-options="c.countryId as (c.countryCallingCode.replace('00', '+') + ' ' + c.countryName) for c in countries track by c.countryId">
    <option value="" disabled selected data-i18n="Country"></option>

关于javascript - angularjs select ngoptions - 以不同的格式显示所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40340714/

相关文章:

javascript - 如何拥有一个 jQuery 插件的多个实例

javascript - 我对它很着迷 : Error: Cannot transition to abstract state

angularjs - 检查 http.get Angular 中执行是否成功

javascript - 在 JavaScript 中更新 'Enable' 的 'asp:DropDownList'

javascript - 在表单提交时动态添加 GET 参数到 URL

HTML/CSS : Vertical border overlapping horizontal border

javascript - 复制内容并带上url

javascript - 使用圆括号访问 json 字符串中的字段

javascript - JavaScript中的动态函数名称

javascript - ng-repeat 中的 Angular 过滤器——没有匹配结果时显示消息