2015-10-07 130 views
3

我正在使用ui-select(https://github.com/angular-ui/ui-select)。如何获取在ui-select搜索框中输入的值?

ui-select支持我们搜索,选择和多选。但是,如何获得在ui-select搜索框中键入的值用户?如果输入的用户值不正确,我想显示一条错误消息。

示例:在下面这个plunker与选择二的主题,当用户键入:“不要找到任何结果” “asdasd”(本文不匹配任何结果)我想显示一条消息通过分配给'scope.person.selected'

plnkr.co/edit/a3KlK8dKH3wwiiksDSn2?p=preview 

我该怎么做?

非常感谢!

回答

0

你可以使用angular-ui-bootstraps的typeahead来达到同样的目的。这里是pluncker链接http://plnkr.co/edit/gsJzdTZHiXZ6MrtTe4F3?p=preview

HTML:

<div class='container-fluid typeahead-demo' ng-controller="TypeaheadCtrl"> 
    <pre>Model: {{asyncSelected | json}}</pre> 
    <input type="text" ng-model="asyncSelected" placeholder="Locations loaded via $http" uib-typeahead="address for address in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" class="form-control"> 
    <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i> 
    <div ng-show="noResults"> 
     <i class="glyphicon glyphicon-remove"></i> No Results Found 
    </div> 
</div> 
+0

谢谢。但我在许多情况下在我的项目中使用了ui-select。改变插件对我来说太复杂了...... – Envy

2

您可以使用ng-keypress="fn($select.search)" at <ui-select...>
,利用此功能,在控制器来获得输入。

$scope.fn = function(search) { 
//do something with search 
} 
3

在angular_ui_select进入$select.search model也使用ng-init="setSelect($select)"输入数据。 您可以使用ui-select-choice的刷新属性。 只需通过refresh="test($select.search)"中的函数,并分别使用refresh-delayminimum-input-length属性设置延迟和最小输入长度。 这里是一个简单的例子:

<ui-select ng-model="model" theme="theme"> 
    <ui-select-match>{{data}}</ui-select-match> 
    <ui-select-choices repeat="options in data" refresh="yourFunction($select.search)" refresh-delay="100" minimum-input-length="1"></ui-select-choices>  
</ui-select> 

我建议你去通过的文件,希望这将有助于。 angular-ui-select

0

您可以通过创建自定义过滤器来完成此操作。在这个例子中, 'uppercasetr'是自定义过滤器。

<ui-select ng-model="vm.mc" theme="select2" limit="10"> 
    <ui-select-match placeholder="Country...">{{$select.selected.SectionTitle}}</ui-select-match> 
    <ui-select-choices repeat="item in vm.data | propsFilter:{SectionTitle : ($select.search | uppercasetr)} | limitTo:$select.limit"> 
     <div ng-bind-html="(item.SectionTitle) | highlight: $select.search"></div> 
     <small> 
      <b>Country:</b> {{item.SectionValue1}} 
     </small> 
    </ui-select-choices> 
</ui-select> 
相关问题