AngularJS - 作为过滤器的数组

时间:2018-06-11 10:20:45

标签: javascript arrays angularjs angular-filters

如何在使用字符串和字符串数组的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种不同的“类型”属性。 我需要放置4个按钮:按钮1到3对应1到3'类型'属性;最后一个按钮必须过滤4到6'类型'属性。 我应该如何使用过滤器作为最后一个按钮?

3 个答案:

答案 0 :(得分:0)

您可以按照以下方式使用过滤器

<div ng-repeat="subject in results.subjects | filter:{grade:'C'}">
   <input ng-model="subject.title" />
</div>

答案 1 :(得分:0)

请参阅下面的代码片段以解析子数组(这仅适用于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>

答案 2 :(得分:0)

对于多个过滤器,您可以使用函数来完成这样的事情

<div ng-repeat="employee in employees | filter:{name: companyName} | filter:gradeFilter">

在控制器中,你的过滤功能有点像

$scope.gradeFilter = function (employee) {
   if(employee.salary > 70000 && employee.exp > 3){ 
      return "Senior"
   } 
}