如何更新动态自动完成的数据列表?

How to update datalist for dynamic auto-completion?

我正在使用数据列表在输入文本中自动完成。 我很好地初始化了它,但我想动态更新我的源代码和我的建议。

你知道怎么做吗?

HTML:

<input type="text" ng-model="title" list="suggestionList" ng-change="changeIsRaised()">
<datalist id="suggestionList">
    <option data-ng-repeat="ttl in titles" value="{{ttl}}">
</datalist>

JavaScript:

$scope.titles = ["Action Comics", "Detective Comics", "Superman", "Fantastic Four", "Amazing Spider-Man"];

$scope.changeIsRaised = function() {
    if ($scope.title == "ch") {
        var newSrc = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];
        $scope.titles = newSrc;

    }
}

您 'nicolas' 列出了两次导致 ng-repeat 错误。您可以添加 track by $index 来修复它,但您可能应该只删除重复项。

function ctrl($scope) {
  $scope.titles = ["Action Comics", "Detective Comics", "Superman", "Fantastic Four", "Amazing Spider-Man"];

  $scope.changeIsRaised = function() {
    if ($scope.title == "ch") {
      var newSrc = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "joseph"];
      $scope.titles = newSrc;

    }
  }
}
angular.module('app', []).controller('ctrl', ctrl);
<body ng-app="app">
  <div ng-controller="ctrl">
    <input type="text" ng-model="title" list="suggestionList" ng-change="changeIsRaised()">
    <datalist id="suggestionList">
      <option data-ng-repeat="ttl in titles" value="{{ttl}}">
    </datalist>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</body>