gpt4 book ai didi

javascript - AngularJS - 根据另一个 md-autocomplete 输入自动更新 md-autocomplete 搜索列表

转载 作者:行者123 更新时间:2023-11-29 17:59:42 28 4
gpt4 key购买 nike

我有两个 <md-autocomplete>下拉菜单。我想根据第一个下拉列表的选择更新第二个下拉列表的搜索列表。

这是一个准备好的非工作插件:http://plnkr.co/edit/GxQujjAcxYdawlANJd9O?p=preview

想要的行为描述:
当用户从第一个下拉菜单中选择“这是 A”时,我想将选项更新为 $scope.numbersA , 即 [1, 2, 3]。对于输入“This is a B”对应的数字数组是$scope.numbersB

我无法让它正常工作,在我的应用程序上,第一个下拉列表中的每个更改都有 $http 请求。如指定here我使用的是 .then() 而不是 .success()。我在 plunker 中简化了示例,因为代码更少:)

编辑:我刚刚注意到 plunker 中的 searchText 无法正常工作,那是因为我的搜索功能。这不是问题,它在我的应用程序上正常运行。

最佳答案

每次用户在第二个自动完成中键入值时,使用md-no-cache调用搜索处理程序。

将第一个自动完成选择的值作为参数传递给第二个自动完成的搜索处理程序

试试这个:

// Code goes here
var app = angular.module('app', ['ngMaterial']);

app.controller('ctrl', ['$scope',
function($scope) {
$scope.letters = [{
'display': 'This is an A',
'value': 'a'
}, {
'display': 'This is a B',
'value': 'b'
}, {
'display': 'This is a C',
'value': 'c'
}];
$scope.numbersA = [1, 2, 3];
$scope.numbersB = [4, 5, 6];
$scope.numbersC = [7, 8, 9];

$scope.getMatchesLetter = function(query) {
if (query === null || query === "" || query === undefined)
return $scope.letters;

var results = query ? $scope.letters.filter(createFilterFor(query)) : $scope.letters;
return results;
};

$scope.getMatchesNumber = function(query, selected) {
var arrToSearch;
switch (selected.value.toUpperCase()) {

case 'A':
arrToSearch = $scope.numbersA;
break;

case 'B':
arrToSearch = $scope.numbersB;
break;

case 'C':
arrToSearch = $scope.numbersC;
break;

}
if (query === null || query === "" || query === undefined)
return arrToSearch;

var results = query ? arrToSearch.filter(createFilterFor(query)) : arrToSearch;
return results;
};

$scope.itemChange = function(item, whichOne) {
switch (whichOne) {
case 'A':
$scope.numbersA = item;
break;
case 'B':
$scope.numbersB = item;
break;
case 'C':
$scope.numberC = item;
break;
}
};

function createFilterFor(query) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(state) {
//return (state.value.indexOf(lowercaseQuery) === 0); // startsWith()
return (state.value.search(lowercaseQuery) != -1); // contains()
};
}

}
]);;
<!DOCTYPE html>
<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>


<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.2/angular-material.min.css">
<script src="script.js"></script>
</head>

<body ng-app='app' ng-controller='ctrl'>
<md-autocomplete md-selected-item="selectedItemLetter" md-search-text="searchTextLetter" md-selected-item-change="itemChange(item)" md-items="item in getMatchesLetter(searchTextLetter)" md-item-text="item.display" md-min-length="0" placeholder="Select letter">
<md-item-template>
<span md-highlight-text="searchTextLetter">{{item.display}}</span>
</md-item-template>
<md-not-found>No matches found.</md-not-found>
</md-autocomplete>

<br>
<br>

<md-autocomplete md-selected-item="selectedItemNumber" md-search-text="searchTextNum" md-selected-item-change="itemChange(item)" md-items="item in getMatchesNumber(searchTextNumber,selectedItemLetter)" md-no-cache="true" md-item-text="item" md-min-length="0"
placeholder="Select number">
<md-item-template>
<span md-highlight-text="searchTextNumber">{{item}}</span>
</md-item-template>
<md-not-found>No matches found.</md-not-found>
</md-autocomplete>
</body>

</html>

关于javascript - AngularJS - 根据另一个 md-autocomplete 输入自动更新 md-autocomplete 搜索列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35888544/

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