0

我正在使用 md-autocomplete 元素,但是当搜索字段为空时没有下拉菜单。它甚至不调用 querySearch 函数。当我输入任何内容时,都会触发该功能并且自动完成功能会起作用。我究竟做错了什么?

的HTML:

        <md-autocomplete
            md-selected-item="selectedItem"
            md-no-cache="true"
            md-search-text="searchText"
            md-items="item in querySearch(searchText)"
            md-item-text="item.name"
            placeholder="Select a Product">
              <span md-highlight-text="searchText">
                 {{ '{{item.originalName}}  ({{item.id}})' }}
              </span>
        </md-autocomplete>

JavaScript:

        function querySearch(query) {
            var results = query ? $scope.products.filter(createFilterFor(query)) : $scope.products;
            return results;
        }


        function createFilterFor(query) {
            var lowercaseQuery = angular.lowercase(query);

            return function filterFn(item) {
                return (angular.lowercase(item.originalName).indexOf(lowercaseQuery) === 0);
            };

        }
4

1 回答 1

5

您应该添加 md-min-length 属性并将其设置为 0

    <md-autocomplete
        md-selected-item="selectedItem"
        md-no-cache="true"
        md-search-text="searchText"
        md-items="item in querySearch(searchText)"
        md-item-text="item.name"
        md-min-length="0"
        placeholder="Select a Product">
          <span md-highlight-text="searchText">
             {{ '{{item.originalName}}  ({{item.id}})' }}
          </span>
    </md-autocomplete>
于 2016-12-12T16:35:30.800 回答