ng-if JSON值不为空

时间:2014-12-20 11:59:10

标签: javascript angularjs

尝试在ng-repeat中显示项目,仅当某个键的值不为空时才会显示。

这是代码:

<a class="item" href="#" ng-repeat="e in events | orderBy:'match_time'" ng-if="e.match_timer !== NaN && e.match_status !== 'FT'">

e.match_timer看起来像:

{
match_timer: ""
}

3 个答案:

答案 0 :(得分:5)

ng-if中你可以像这样编写你的条件

<a class="item" href="#" ng-repeat="e in events | orderBy:'match_time'" ng-if="e.match_timer !== '' && e.match_status !== 'FT'">

这样它只会显示e.match_timer非空的元素,而其他值则为FT

答案 1 :(得分:2)

您可以检查字符串的长度。

e.match_timer.length > 0 or e.match_timer > 0 if it is number.

答案 2 :(得分:1)

而不是ng-if,您应该使用ng-filter,就像这样

Html:

<a class="item" href="#" ng-repeat="e in events | orderBy:'match_time' | filter:filterExpression">{{e.match_timer}}</a>

JS:

myApp.filter('filterExpression', function() {
  return function (e) {
    return typeof (e.match_timer) === 'string' && e.match_status !== 'FT'
  };
});

演示:http://jsfiddle.net/HB7LU/9381/