簡體   English   中英

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種不同“類型”屬性的主要對象數據。 我需要放置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"
   } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM