【问题标题】:Angular typeahead: How to separate label from model?Angular typeahead:如何将标签与模型分开?
【发布时间】:2016-05-31 05:38:27
【问题描述】:

我在 UI 网格中使用来自 angular-ui 的 typeAheads。 uib-typeahead 中的标签和 ng-model 之间似乎存在隐含联系。

<input type="text" ng-model="selected" uib-typeahead="country as country.id for country in countrys | filter:$viewValue" typeahead-on-select="onSelect($item, $model, $label)" class="form-control">

在这种情况下,country.id 表示选择后输入中显示的值是 selected.id。但这没有明确定义。

但是,如果 selected 没有名为 id 的字段,则会导致输入显示 [Object object]。

我可以将模型与显示的内容和标签分离吗?

这是一个小笨蛋。我希望预先输入列出 id,但我希望输入显示模型的值。 http://plnkr.co/edit/EnOefFQVcTBoaHxiCc5I?p=preview

编辑:我更改了 plunked 以更好地反映我的问题。如果我将 ng-model 设置为 selected.value 我会在开始时得到一个很好的结果,但是如果我改变这个值我会在 selected.value 中得到一个嵌套的 JSON >

【问题讨论】:

    标签: javascript angularjs angular-ui-typeahead


    【解决方案1】:
    uib-typeahead="country as country.value for country in countrys | filter:$viewValue"
    

    您应该使用这样的 typeahead 来更好地了解您的 typeahdead。 但是,如果您仍想将其与 id 一起使用,则可以使用 input-formatter。 typehead 的这个属性定义了你选择的对象是如何显示的。

    function(ojb) {
    return obj.id ? obj.id : obj.value;
    }
    

    如果有则返回id,否则返回所选对象中的值。

    【讨论】:

      猜你喜欢
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-13
      • 2021-05-11
      • 2011-07-12
      • 1970-01-01
      • 2019-04-04
      • 2014-05-27
      相关资源
      最近更新 更多