Angular Material 自动完成无数据或未找到消息

Angular Material autocomplete no data or not-found-message

我的 md-autocomplete 没有同时显示数据和 md-not-found 数据。

angular.module('BlankApp').controller('ctrl', function($scope, $q){
    $scope.items = [{name: 'item1', id: 1}, {name: 'item2', id: 2}, {name: 'item3', id: 3}];

    $scope.promisedItems = function(){
        var deferred = $q.defer();
        deferred.resolve(items);
        return deferred.promise;
    }
});

<md-autocomplete md-selected-item="selectedItem2" md-search-text="searchText2" md-items="item in promisedItems()" md-item-text="item.name" md-min-length="0" placeholder="items">
    <md-item-template>
        <span md-highlight-text="searchText2" md-highlight-flags="^i">{{item.name}}</span>
    </md-item-template>
    <md-not-found>
        No states matching "{{searchText2}}" were found.
    </md-not-found>
</md-autocomplete>

Check the following codepen

Attempt1 显示数据但未显示 'not-found-message'。
Attempt2 确实显示 'not-found-message' 但不会显示数据。

我把它包裹在一个承诺中。
我怎样才能让他们同时工作?

事实证明,md-autocomplete 不会为您执行 'search work'。 您需要实施搜索逻辑和 return 找到的项目。

我将我的搜索逻辑(又名过滤器)设为通用,因此您不必制作大量搜索逻辑。

    $scope.searchCollection = function (searchString, collection, propertyName) {
        var deferred = $q.defer();
        var result = searchString ? collection.filter(createFilterFor(searchString, propertyName)) : collection;
        deferred.resolve(result);
        return deferred.promise;
    }

    function createFilterFor(searchString, propertyName) {
        var lowercaseQuery = angular.lowercase(searchString);
        return function filterFn(item) {
            return (angular.lowercase(item[propertyName]).indexOf(lowercaseQuery) === 0);
        };
    }

用法md-items="item in searchCollection(searchText, myCollection, 'propertyNameToSearchIn')"