gpt4 book ai didi

javascript - 使用过滤器更新范围对象 - AngularJS

转载 作者:行者123 更新时间:2023-11-28 07:38:48 26 4
gpt4 key购买 nike

我需要在更改文本字段上将分配过滤对象从 $scope.Departments 更新为 $scope.FilteredDepartments

HTML:

<input type="text" ng-change="searchItems(searchquery)" ng-model="searchquery">

Angular 代码:

$scope.Departments= [{id: 1, name:'ABC'},{id: 2, name:'XYZ'}...];

$scope.searchItems = function (searchString) {
//Need to do something here
}

如何使用名称进行过滤并将过滤结果分配给 $scope.FilteredDepartments

感谢帮助

最佳答案

HTML:

<input type="text" ng-model="searchValue">
<h3>Filter Ones</h3>
<div ng-repeat="department in filteredDepartments">
<p>{{department.name}}</p>
</div>

JS:

$scope.departments= [{id: 1, name:'ABC'},{id: 2, name:'XYZ'}];

$scope.$watch(function () {
return $scope.searchValue;
}, function () {
$scope.filteredDepartments = $filter('filter')($scope.departments, $scope.searchValue);
});

不要忘记将 $filter 注入(inject) Controller

关于javascript - 使用过滤器更新范围对象 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299388/

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