[英]Angular JS ng-if not working within ng-repeat
我在作用域中有一系列對象,正在使用ng-repeat遍歷。 我想根據它們的值過濾掉一些元素,但似乎ng-if被完全忽略了。
我設置了這個簡單的插件來說明: http : //plnkr.co/edit/Aj0hpZQTfnkQ6BYG8DRb 。
這是控制器:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.events = [
{key: "ClassName", value: "exception", age:12},
{key: "Message", value: "oops", age: 25},
{key: "Stack", value: null, age: 35}
]
});
和HTML模板:
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<ul>
<li ng-repeat="event in events" ng-if="event.age > 20">
[{{$index + 1}}] {{event.key}}: {{event.value}} - {{event.age}}
</li>
</ul>
</body>
</html>
應該使用ng-if
指令過濾掉數組的第一個元素,無論如何它都會顯示出來。 我錯過了忽略ng-if的哪些內容?
我想正確的方法是使用過濾器,例如:
<li ng-repeat="event in events | filter:greaterThan">
我經常使用兩個自定義的角度模塊, FilterIncludeIf
和UtilCompare
提供ng-if
樣的功能, ng-repeat
秒。
FilterIncludeIf
具有UtilCompare
作為依賴項,並接受兩個參數。 第一個是字符串形式的比較運算符(在您的情況下為">="
)。
第二個是Object
。 鍵應設置為從ng-repeat
語句返回的單個對象的鍵(在下面的示例中為customer
),您要比較其值。 值應設置為您要比較的值。
查看example-snippet.html
, includeIf
最終過濾了customers.all
並確保呈現的唯一customer
是以下所有語句均為true
。
customer[firstname] != customers.all.current.data.firstname`
customer[lastname] != customers.all.current.data.lastname`
customer[email] != customers.all.current.data.email`
例如-snippet.html
<li
ng-repeat="customer in customers.all | filter:query | includeIf : '!=' : {
firstname : customers.current.data.firstname,
lastname : customers.current.data.lastname,
email : customers.current.data.email,
}"
class="tab"
>
<div class="info">
<h4 class="subheader name">
{{customer.firstname}} {{customer.lastname}}
</h4>
{{customer.email}}
</div>
</li>
篩選-includeIf
angular
.module('FilterIncludeIf', [
'UtilCompare'
])
.filter('includeIf', function (compare) {
return function (items, comparator, attrs) {
var filteredInput = [];
for (var i = 0; i < items.length; i++) {
var item = items[i],
numOfAttrs = 0,
numOfMatchedAttrs = 0;
for (var key in attrs) {
numOfAttrs++
var value = attrs[key],
comparison = compare
.these(item[key], value)
.by(comparator)
.eval();
if (comparison) numOfMatchedAttrs++;
}
if (numOfAttrs == numOfMatchedAttrs) filteredInput.push(item);
}
return filteredInput
};
});
工廠-比較
angular
.module('UtilCompare', [])
.factory('compare', function () {
var _by = undefined,
_a = undefined,
_b = undefined;
var comparators = {
'>' : function(c, d) { return c > d },
'<' : function(c, d) { return c < d },
'>=' : function(c, d) { return c >= d },
'<=' : function(c, d) { return c <= d },
'!=' : function(c, d) { return c != d },
'!==' : function(c, d) { return c !== d },
'==' : function(c, d) { return c == d },
'===' : function(c, d) { return c === d },
}
function by (comparator) {
_by = comparator;
return this
}
function these (a, b) {
_a = a;
_b = b;
return this
}
function eval () {
return comparators[_by](_a, _b);
}
return {
by : by,
these : these,
eval : eval
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.