Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击项目时,AngularUI Boostrap typeahead不会选择_Javascript_Angularjs_Twitter Bootstrap_Angular Ui - Fatal编程技术网

Javascript 单击项目时,AngularUI Boostrap typeahead不会选择

Javascript 单击项目时,AngularUI Boostrap typeahead不会选择,javascript,angularjs,twitter-bootstrap,angular-ui,Javascript,Angularjs,Twitter Bootstrap,Angular Ui,我是AngularUI的新手,在AngularUI引导工具包中单击下拉列表中的某个项目时,不会用完整值填充文本框。单击时,下拉框消失,只保留键入的字符 这是开始在文本框中键入spy的代码 问题似乎出在typeaheadLabel函数中,该函数在计算模型值时进行求值,并且该项通常为null。您可以在label函数上添加一些防御性的空检查,这样值计算就不会被破坏,因为typeahead实际上匹配模型的值,如下所示:- 来自TypeAhead的片段 我建议的另一种方法是在viewModel本身中添加d

我是AngularUI的新手,在AngularUI引导工具包中单击下拉列表中的某个项目时,不会用完整值填充文本框。单击时,下拉框消失,只保留键入的字符

这是开始在文本框中键入spy的代码


问题似乎出在typeaheadLabel函数中,该函数在计算模型值时进行求值,并且该项通常为null。您可以在label函数上添加一些防御性的空检查,这样值计算就不会被破坏,因为typeahead实际上匹配模型的值,如下所示:-

来自TypeAhead的片段

我建议的另一种方法是在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指定为alias asset.ticker作为asset中资产的asset.displayName


谢谢,这很有帮助!您的第二个解决方案displayName仅适用于Angular 1.2,而不是当前的1.3.x测试版。我不知道为什么,但一旦我改变了这一点并合并了您的解决方案,一切都如Plnkr所示。
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])
      };
  $scope.typeaheadLabel = function(item) {
    if(!item || !item.ticker) return;
    return item.ticker.toUpperCase() + ' (' + item.assetClass + ') - ' + item.description;
  };
   $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;
      };