且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular-UI typeahead:显示标签但仅绑定到值

更新时间:2022-06-25 03:40:03

这并不理想,但 typeahead-input-formatter 属性提供了一种变通方法,直到可以提供修复为止.(Plunker 来自 github 线程).

It's not ideal, but the typeahead-input-formatter attribute provides a work-around until a fix can be provided. (Plunker from github thread).

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 控制器功能:

AngularJs controller function:

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