[英]AngularJS how to filter an element in an array
我有這么大
$scope.arrVaritions = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];
如何在此數組中找到元素索引? 我正在使用此代碼,但是它不起作用:
$scope.elementId = 1;
$filter('filter')($scope.arrVaritions, { $scope.elementId });
嘗試像下面的演示小提琴一樣以正確的方式對其進行過濾:
<div ng-controller="MyCtrl">
{{filtered}}
</div>
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function ($scope, $filter) {
$scope.arrVaritions = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];
$scope.elementId = 1;
$scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId, true);
});
如果您不想過濾確切的值,只需刪除第三個true
選項:
$scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId);
它應該只有$scope.elementId
而不是object,並且第三個選項true
才能匹配精確值。 否則,沒有true
,如果elementId
會考慮10
, 11
, 100
,等值(它會做包含檢查)。
$filter('filter')($scope.arrVaritions, $scope.elementId, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.