【发布时间】:2016-12-06 02:09:15
【问题描述】:
我正在 AngularJS 中尝试使用动态过滤器(位置 - 值,如 US、IN、CA 等)显示员工详细信息,作为基于从 DB 获取的数据的复选框列表。我尝试了多种方法都没有成功。请帮助实现 Checkboxlist 中的动态过滤器。
我的代码示例如下:
<html>
<body ng-app="myapp" ng-controller="myController">
<div >Location</div>
<table>
<tbody>
<tr ng-repeat="empL in EmpResult | unique : 'Location'">
<td>
<span>
<input type="checkbox" ng-model="loc" value={{empL.Location}} />
{{empL.Location}}
</span>
</td>
</tr>
</tbody>
</table>
<table align="left" style="width: 100%" class="table">
<thead>
<tr>
<th align="left" style="width: 30%">Employee</th>
<th align="left" style="width: 20%">Address</th>
<th align="left" style="width: 15%">Location</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="empN in EmpResult | filter : loc">
<td align="left" style="width: 30%">{{empN.EmpName}}</td>
<td align="left" style="width: 10%">{{empN.Address}}</td>
<td align="left" style="width: 15%">{{empN.Location}}</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
var myapp = angular.module('myapp', ['ui.unique'])
.controller("myController", function ($scope, $http) {
$http({
method: 'Get',
params: { strName: $scope.strName },
url: 'Emp.asmx/GetEmpbyName'
}).then(function (response) {
$scope.EmpResult = response.data;
})
});
</script>
</body>
</html>
【问题讨论】:
-
据我所知,过滤器不是这样工作的。您必须定义一个实际的过滤器方法(而不仅仅是像您那样使用范围变量)。检查文档。在这里,您有一个很好的描述和非常简单的示例,非常符合您的需求:docs.angularjs.org/api/ng/filter/filter
-
@FDavidov 是的,但@Ravi 使用的过滤器来自他在模块中注入的依赖项
'ui.unique',因此不需要定义。但是这个依赖已经贬值了,所以我的问题是为什么要使用贬值的依赖?
标签: javascript angularjs filter checkboxlist