3

我正在尝试在我的网站中使用角度材料的自动完成组件。

在我的html代码中:

     <md-autocomplete md-selected-item="selectedItem" md-search-text="searchText" md-items="item in getMatches(searchText)" md-item-text="item.display">
          <md-item-template>
              <span md-highlight-text="searchText">{{item.display}}</span>
          </md-item-template>
          <md-not-found>
              No matches found.
          </md-not-found>
      </md-autocomplete>

在我的控制器中,我有以下内容:

app.controller('IndexController', function ($scope) {
    $scope.getMatches = function (text) {
        alert(text);
    }
}); 

如您所见,我没有实施太多。但如果自动完成功能正在尝试查找某些内容,它应该执行 getMatches 并提醒文本。

在我的场景中,它除了打印“未找到匹配项”之外什么都不做。

没有文本输入来输入要搜索的文本。

我错过了什么?

jsfiddle 在https://jsfiddle.net/p7wc8psy/

4

2 回答 2

3

您制作的 DOM 是正确的。

 <md-autocomplete md-selected-item="selectedItem" md-search-text="searchText" md-items="item in getMatches(searchText)" md-item-text="item.display">
      <md-item-template>
          <span md-highlight-text="searchText">{{item.display}}</span>
      </md-item-template>
      <md-not-found>
          No matches found.
      </md-not-found>
  </md-autocomplete>

但是下面显示的函数是错误的,因为它没有返回任何内容,这就是您得到 “未找到匹配项”的原因。

app.controller('IndexController', function ($scope) {
    $scope.getMatches = function (text) {
        alert(text);//this does not return anything
    }
}); 

现在下一个问题应该返回什么。

它应该返回一个如下所示的 JSON 数组。

[{
        value: "apple",
        display: "apple"
    }, {
        value: "banana",
        display: "banana"
    }, {
        value: "gauva",
        display: "gauva"
    }, {
        value: "melon",
        display: "melon"
    }, {
        value: "potato",
        display: "potato"
    }, {
        value: "carrot",
        display: "carrot"
    }, {
        value: "cauliflower",
        display: "cauliflower"
    }, {
        value: "jasmine",
        display: "jasmine"
    }, {
        value: "cabbage",
        display: "cabbage"
    }, {
        value: "peas",
        display: "peas"
    }]

display keyJSON 中的内容是什么

由于您在此处提到,md-item-text="item.display"因此返回的数组必须具有显示在自动完成下拉菜单中的显示键。

所以我的搜索功能是这样的:

$scope.myDta = [{
            value: "apple",
            display: "apple"
        }, {
            value: "banana",
            display: "banana"
        }, {
            value: "gauva",
            display: "gauva"
        }, {
            value: "melon",
            display: "melon"
        }, {
            value: "potato",
            display: "potato"
        }, {
            value: "carrot",
            display: "carrot"
        }, {
            value: "cauliflower",
            display: "cauliflower"
        }, {
            value: "jasmine",
            display: "jasmine"
        }, {
            value: "cabbage",
            display: "cabbage"
        }, {
            value: "peas",
            display: "peas"
        }];
        $scope.getMatches = function (text) {
            text = text.toLowerCase();
            var ret = $scope.myDta.filter(function (d) {
                //return element which starts with entered text
                return d.display.startsWith(text);
            });
            return ret;
        }

工作代码在这里

测试用例:类型ca

希望这可以帮助!

于 2015-11-29T08:08:53.147 回答
0

我不能让你的小提琴奏效。我已经努力到达https://jsfiddle.net/p7wc8psy/7/但我认为你需要最新的角度来运行材料,这很难在 jsFiddle 中加载。您可能想要切换到 codepen 或其他东西。

同时,我认为您缺少的是:

<md-autocomplete 
     name="search-drink-autocomplete-input" 
     md-selected-item="selectedItem" 
     md-search-text="searchText" 
     md-items="item in getMatches(searchText)" 
     md-item-text="item.display"
     md-search-text-change="getMatches(searchtext)">  // *********
于 2015-11-28T11:00:45.947 回答