gpt4 book ai didi

angularjs - 如何使用 typeahead 属性估计文本框的初始值?

转载 作者:行者123 更新时间:2023-12-02 23:21:23 25 4
gpt4 key购买 nike

我在我的 angularjs 项目中使用 typeahead。

我将此值显示在带有 typeahead 属性的文本框中:

  $scope.cities = [{id:1,address:'Berlin'},
{id:2,address:'Bonn'},
{id:3,address:'London'},
{id:4,address:'Miami'}];

这是带有 typeahead 属性的输入文本框:

<input type="text" 
ng-model="city"
typeahead-input-formatter="inputFormatter($model)"
placeholder="Custom template"
uib-typeahead="city as city.address for city in cities | filter:$viewValue"
class="form-control"
typeahead-show-hint="true"
typeahead-min-length="0"/>

这里是plunker .

如何为包含城市的文本框设置初始值,例如 id=3。

最佳答案

如果您需要设置默认选定值,那么您可以根据您的情况为模型分配一个值:$scope.city (如在 HTML 中,您有 <input type="text" ng-model="city" .. ),如下所示:

$scope.selected = {id:1, address:'Berlin'};
$scope.cities = [{id:1, address:'Berlin'},
{id:2,address:'Bonn'},
{id:3,address:'London'},
{id:4,address:'Miami'}];
//assign model value
$scope.city = $scope.cities[3]
//or do like this - $scope.city = $scope.selected;

检查一下:http://plnkr.co/edit/zKkIbyGYetxQejyfBSnG?p=preview

如果不是按 id 排序,那么你仍然可以通过 id 找到它(假设你使用 lodash ,如果没有,你可以使用 Array.forEach ):

var id_i_need = 42;
var defaultItem = _.find($scope.cities, function(item){
return item.id == id_i_need;
})[0]
$scope.city = defaultItem;

关于angularjs - 如何使用 typeahead 属性估计文本框的初始值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37133676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com