Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – Angular-UI typeahead:显示标签,但绑定到值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Angular-UI typeahead在以下方式:
<input type="text" ng-model="mymodel" typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5" typeahead-editable="false" />

绑定到一个模型像:

var options = [
    {"value": 1,"text": "value1"},{"value": 2,"text": "value2"},...
];

它正确显示选项文本,但是当我选择一个项目时,它在文本框内显示值。模型正确地绑定到值(而不是整个模型对象)。

是否可以在选择后在文本框内显示“文本”(而不是“值”),仍然保持模型绑定只是值(即:当我选择一个特定的“文本”模型更新为“值” )?

Thanx

这不是理想的,但是typeahead-input-formatter属性提供了一个解决方法,直到可以提供修复。 ( Plunkergithub螺纹)。

HTML:

<input type="text" ng-model="mymodel" typeahead="o.value as o.text for o in options | filter:$viewValue | limitTo:5" typeahead-editable="false" typeahead-input-formatter="formatLabel($model)" />

AngularJs控制器功能

$scope.formatLabel = function(model) {
   for (var i=0; i< $scope.options.length; i++) {
     if (model === $scope.options[i].value) {
       return $scope.options[i].text;
     }
   }
};

大佬总结

以上是大佬教程为你收集整理的angularjs – Angular-UI typeahead:显示标签,但绑定到值全部内容,希望文章能够帮你解决angularjs – Angular-UI typeahead:显示标签,但绑定到值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。