如何在Angularjs中获得0值长度?

时间:2017-08-22 07:29:44

标签: html css angularjs

如何在angularjs中获得问题open eyes 0 values length

My Plunker

  • 在我的关键词中,我得到了以下解决方案:(i)完全没有问题(ii)总票数和(iii)睁眼总值[值1],我们期望的是(iv)如何得到Open Eyes 0个值length

  • 如何获取Open Eyes [0值] length,请查看my plunker以供参考我不知道我在哪里犯了错误

  • 如果我使用{{question.openeyes.length == '0'}我得到False的答案, 期待的答案是:2

我的Html: -

<p>Total no of questions :{{question.length}} </p>

<p>Total no of upvotes : {{resultValue | sumOfValue:'upvotes'}}</p>

<p>Total no of Open Eyes of [1 values] : {{resultValue | sumOfValue:'openeyes'}}</p>

<p class="color">Total no of Open Eyes of [0 value] : {{question.openeyes.length == '0'}}</p>

我的数据:

    $scope.question = [
   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": ["sarawana@gmail.com"],
       "openeyes": 1,
       "upvoters": ["sarawana@gmail.com"],
       "upvotes": 1,
       "title": "what is cricket",
       "created": "2017-06-06T12:58:40.204Z"
},

   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": [],
       "openeyes": 0,
       "upvoters": ["sarawana@gmail.com"],
       "upvotes": 1,
       "title": "who fan you are",
       "created": "2017-06-06T12:58:40.204Z"
},

   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": [],
       "openeyes": 0,
       "upvoters": [  "ms@e21designs.com", "vp@gmail.com"],
       "upvotes": 2,
       "title": "best of the day",
       "created": "2017-06-06T12:58:40.204Z"
},
{
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": ["ms@e21designs.com"],
       "openeyes": 1,
       "upvoters": ["ms@e21designs.com","vp@gmail.com", "ms@gmail.com"],
       "upvotes": 0,
       "title": "he is best",
       "created": "2017-06-06T12:58:40.204Z"
}]
  • 如果有人知道解决方案,请更新my plunker以了解确切的解决方案,谢谢

1 个答案:

答案 0 :(得分:1)

您可以创建过滤器或调用函数以获得Open Eyes [0值] length的长度。但是使用过滤器是更优选的,因为后者存在性能问题。

方法1 直接调用函数

<强> JS

  $scope.openZero=function(q){
    var count =0;
    for(var i=0;i<q.length;i++){
      if(q[i].openeyes==0)
       count ++;         
    }
    return count;
  }

<强> HTML

<p class="color">Total no of Open Eyes of [0 value] : {{openZero(question)}}</p>

Working Plunker http://plnkr.co/edit/7XedtmD6JlqpkgM3B7on?p=preview

<小时/> 方法2 使用过滤器

<强> JS

.filter('sumOfZero', function() {
            return function(data, key) {
                if (angular.isUndefined(data) && angular.isUndefined(key)) return 0;
                var count = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].openeyes == 0) count++;
                }
                return count;
            }

<强> HTML

<p class="color">Total no of Open Eyes of [0 value] : {{resultValue | sumOfZeros:'openeyes'}}</p>

Working Plunker http://plnkr.co/edit/RjNWeqWEYuzbBdew8s7V?p=preview