selectMatch上的angular ui-bootstrap typeahead回调?

mch*_*les 92 angularjs bootstrap-typeahead angular-ui

我正在使用角度ui-bootstrap typeahead,我想用它作为一种方法来获取很多选择,所以我需要在selectMatch方法启动时获取所选值但我找不到怎么做在我的控制器中

<div class='container-fluid' ng-controller="TypeaheadCtrl">
<pre>Model: {{selected| json}}</pre>
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue">
Run Code Online (Sandbox Code Playgroud)

如果我看到所选的值,每按一次键我都会得到更改...

scope.$watch('selected', function(newValue, oldValue) {... });
Run Code Online (Sandbox Code Playgroud)

我知道方法selectMatch是当用户按下回车或点击列表时调用的方法,但我不知道如何对其进行回调...

谢谢 !

Mat*_*att 250

现在有更好的方法.添加了一个新的回调方法

在控制器文件中添加以下内容:

 $scope.onSelect = function ($item, $model, $label) {
    $scope.$item = $item;
    $scope.$model = $model;
    $scope.$label = $label;
};
Run Code Online (Sandbox Code Playgroud)

在视图中添加以下内容:

 <input type="text"
        ng-model="selected"
        typeahead="state for state in states | filter:$viewValue"
        typeahead-editable="false"
        typeahead-on-select="onSelect($item, $model, $label)"/>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅typeahead规范(搜索onSelect).

查看以下网址是否有助于 http://www.techguides.in/how-to-create-autocomplete-using-angularjs-ui/

http://www.techguides.in/how-to-customize-autocomplete-to-display-multiple-columns-using-angularjs-ui-2/

  • 有没有人知道$ item $ model $ label对象实际上在回调类型中是什么?onalect-on-select ='onSelect($ item,$ model,$ label)'? (28认同)
  • @ AardVark71更容易解释表达式是否类似:`state.id as state.name for state in states`.在这种情况下,`$ item`是`state`,$ model是`state.id`,`$ label`是`state.name` (16认同)
  • @AardVark71 $ item $ model $ label这三个属性分别如下所示.如果要绑定具有多个属性的JSON对象数组,则将在$ item中获取具有所有属性的选定项.$ model是内置的角度模型,用于存储所选的item.value和$ lable将为您提供选择后文本框中显示的值.所以总之很多时候,$ label将等于$ model.希望这能澄清你的怀疑. (15认同)
  • 谢谢!工作就像一个魅力.这可能应该在Typeahead标题下的参考页面上正式记录:http://angular-ui.github.io/bootstrap/ (2认同)

mch*_*les 10

编辑:这个方法现在不是最好的方法.最好使用onSelect回调,如上面的答案中所解释的那样.

我发现如何做我想做的事情.我确实看到有一个类型可编辑的属性,如果它设置为false,那么只有选择了模型中的值时,所选值才会更改.因此$ watch正常工作以检查何时选择新值.

<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue" typeahead-editable="false">

link: function(scope, elm, attrs){
    scope.$watch('selected', function(newValue, oldValue) {
        if (newValue)
          console.log(oldValue+"->"+newValue);
     });
}
Run Code Online (Sandbox Code Playgroud)