md autocomplete 在输入为空时不显示任何结果

md autocomplete shows no results when input is empty

我正在使用 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);
            };

        }

您应该添加 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>