我是Angular的新手,但AngularUI引导程序工具包存在问题,在该工具包中单击下拉列表中的项目不会使文本框充满完整值。单击时,下拉框消失,仅保留键入的字符。

这是代码(开始在文本框中输入“间谍”)

http://plnkr.co/edit/WYLn0c6HvuOLl1pJBCxa?p=preview

<body>
  <div data-ng-controller="AssetCtrl">
    <br />
    <input type="text" ng-model="selected" typeahead="asset.ticker as typeaheadLabel(asset) for asset in assets | filter:{ticker:$viewValue}" class="form-control">

  </div>



  <script>
    var ConsoleApp = angular.module('ConsoleApp', ['ui.bootstrap']);
    function AssetCtrl($scope) {
      $scope.assets = [{
        "assetClass": "stock",
        "ticker": "spy",
        "description": "S&P"
      }];

      $scope.typeaheadLabel = function(item) {
        return item.ticker.toUpperCase() + ' (' + item.assetClass + ') - ' + item.description;
      };
    }

    ConsoleApp.controller('AssetCtrl', AssetCtrl);
  </script>
</body>

最佳答案

看来问题出在typeaheadLabel函数,该函数在计算模型值时得到评估,而item通常为null。您可以在label函数上添加一些防御性的null检查,以使值求值不会中断,因为这是typeahead实际上与模型值匹配的内容:-

来自TypeAhead的摘要

return {
        itemName:match[3],
        source:$parse(match[4]),
        viewMapper:$parse(match[2] || match[1]), //<-- here if the function evaluation is undefined which is in your case it will take the value of ticker propery as modelValue
        modelMapper:$parse(match[1])
      };


WorkAround1:-

  $scope.typeaheadLabel = function(item) {
    if(!item || !item.ticker) return;
    return item.ticker.toUpperCase() + ' (' + item.assetClass + ') - ' + item.description;
  };


Plnkr

我建议的另一种方法是在viewModel本身中添加displayName属性。例如:-

解决方法2:

   $scope.assets = [{
        "assetClass": "stock",
        "ticker": "spy",
        "description": "S&P"
      },{
        "assetClass": "stock",
        "ticker": "asd",
        "description": "A&D"
      }].map(typeaheadLabel);


      function typeaheadLabel(item) {
        item.displayName = item.ticker.toUpperCase() + ' (' + item.assetClass + ') - ' + item.description;
        return item;
      };


并指定displayName作为别名asset.ticker as asset.displayName for asset in assets

Plnkr2

关于javascript - 单击项目时不会选择Angular UI Bootstrap typeahead,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24900117/

10-13 02:48