gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:57:17 25 4
gpt4 key购买 nike

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

你知道如何做这样的事情吗?

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>

关于javascript - 如何更新动态自动完成的数据列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38315256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com