AngularJS - 数组作为过滤器
AngularJS - Array as filter
如何在使用字符串和字符串数组的 ng-repeat
中设置过滤器?
HTML
<div ng-repeat="type in types| orderBy : 'name' | filter:{typology:currTipology}"
JavaScript
var myData = ['string_1', 'string_2', ['string_3', 'string_4']];
我将为每个 myData
条目使用一个按钮,将字符串值分配给“currTipology”。
更新
我试图更好地解释我的问题。
我有一个具有 6 个不同 'typology' 属性的主要对象数据。
我需要放置4个按钮:按钮1到3对应1到3 'typology'属性;最后一个按钮必须过滤 4 到 6 'typology' 属性。
我应该如何为最后一个按钮使用过滤器?
您可以通过以下方式使用过滤器
<div ng-repeat="subject in results.subjects | filter:{grade:'C'}">
<input ng-model="subject.title" />
</div>
请参阅下面的代码片段来解析子数组(这仅适用于 2 级深度数组)。希望它能完成工作:)
var app = angular.module("app", []);
app.controller("AppCtrl", function ($scope) {
$scope.myData = ['string_1', 'string_2', ['string_3', 'string_4']];
$scope.isArray = function (type) {
return typeof type === 'object';
}
$scope.isString = function (type) {
return typeof type === 'string';
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="AppCtrl">
<div ng-repeat="type in myData">
<div ng-if="isString(type)">
{{type}}
</div>
<div ng-if="isArray(type)" ng-repeat="subtype in type">
{{subtype}}
</div>
</div>
对于多重过滤器,你可以像这样用函数做一些事情
<div ng-repeat="employee in employees | filter:{name: companyName} | filter:gradeFilter">
并且在控制器中你有你的过滤功能,比如
$scope.gradeFilter = function (employee) {
if(employee.salary > 70000 && employee.exp > 3){
return "Senior"
}
}
如何在使用字符串和字符串数组的 ng-repeat
中设置过滤器?
HTML
<div ng-repeat="type in types| orderBy : 'name' | filter:{typology:currTipology}"
JavaScript
var myData = ['string_1', 'string_2', ['string_3', 'string_4']];
我将为每个 myData
条目使用一个按钮,将字符串值分配给“currTipology”。
更新 我试图更好地解释我的问题。 我有一个具有 6 个不同 'typology' 属性的主要对象数据。 我需要放置4个按钮:按钮1到3对应1到3 'typology'属性;最后一个按钮必须过滤 4 到 6 'typology' 属性。 我应该如何为最后一个按钮使用过滤器?
您可以通过以下方式使用过滤器
<div ng-repeat="subject in results.subjects | filter:{grade:'C'}">
<input ng-model="subject.title" />
</div>
请参阅下面的代码片段来解析子数组(这仅适用于 2 级深度数组)。希望它能完成工作:)
var app = angular.module("app", []);
app.controller("AppCtrl", function ($scope) {
$scope.myData = ['string_1', 'string_2', ['string_3', 'string_4']];
$scope.isArray = function (type) {
return typeof type === 'object';
}
$scope.isString = function (type) {
return typeof type === 'string';
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="AppCtrl">
<div ng-repeat="type in myData">
<div ng-if="isString(type)">
{{type}}
</div>
<div ng-if="isArray(type)" ng-repeat="subtype in type">
{{subtype}}
</div>
</div>
对于多重过滤器,你可以像这样用函数做一些事情
<div ng-repeat="employee in employees | filter:{name: companyName} | filter:gradeFilter">
并且在控制器中你有你的过滤功能,比如
$scope.gradeFilter = function (employee) {
if(employee.salary > 70000 && employee.exp > 3){
return "Senior"
}
}