gpt4 book ai didi

javascript - 如何将 js 变量输入到 ng-repeat 中的过滤器中

转载 作者:行者123 更新时间:2023-12-03 01:28:53 24 4
gpt4 key购买 nike

我有这个:

<div ng-repeat="i in MediaFeedItems | filter: { category: 'userSelect' }" class="mediafeed--item"> 

这会迭代列表并根据名为“类别”的属性进行过滤。用户通过下拉菜单选择过滤器,并将该选择分配给以下函数中的变量“userSelect”:

document.getElementById('category').addEventListener("change", function() {
if(this.value == "HR") {
this.userSelect = "HR";
} else if (this.value == "Managers") {
this.userSelect = "Managers";
} else {
this.userSelect = "Colleagues";
}
});

如何在过滤器中调用该变量?

最佳答案

认为您不需要该功能,只需在您的选择上放置一个 ng 模型并将其用于您的过滤器

(function() {
'use strict';
angular
.module('myApp', [])
.controller('myCtrl', myCtrl);

function myCtrl(){
/* jshint validthis: true */
var vm=this;
vm.items = [
{category: 'cat1', text: 'one'},
{category: 'cat1', text: 'two'},
{category: 'cat2', text: 'three'},
{category: 'cat2', text: 'four'},
];
}

})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
<div ng-controller="myCtrl as vm">
<select name="singleSelect" ng-model="category">
<option value="cat1">category 1</option>
<option value="cat2">category 2</option>
</select>
<div class="row" ng-repeat="item in vm.items | filter: { category: category }">
{{item.text }}
</div>
</div>
</div>

关于javascript - 如何将 js 变量输入到 ng-repeat 中的过滤器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51383328/

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