最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng-if JSON value not empty - Stack Overflow

programmeradmin3浏览0评论

try to show items in ng-repeat, only if value of some key not empty.

This is code:

<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 looks like:

{
match_timer: ""
}

try to show items in ng-repeat, only if value of some key not empty.

This is code:

<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 looks like:

{
match_timer: ""
}
Share Improve this question asked Dec 20, 2014 at 11:59 n00b43677n00b43677 1872 silver badges16 bronze badges 3
  • 1 try using ng-if="e.match_timer !==''" – Arjun Vachhani Commented Dec 20, 2014 at 12:06
  • thanks, it works! I tried "" but it not working – n00b43677 Commented Dec 20, 2014 at 12:17
  • if you are using double quotes(") then you should use single quotes for directive like ng-if='e.match_timer !==""' or to escape double quotes(") you can use slash (\) – Arjun Vachhani Commented Dec 20, 2014 at 12:20
Add a ment  | 

3 Answers 3

Reset to default 5

in ng-if you can write your condition like this

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

This way it will display only those elements which are non-empty for e.match_timer and has other value then FT

You can check for the length of the string.

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

Instead of ng-if you should use a ng-filter, like this

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'
  };
});

Demo: http://jsfiddle/HB7LU/9381/

发布评论

评论列表(0)

  1. 暂无评论