R. Mani Selvam R. Mani Selvam - 2 months ago 17
HTML Question

How to get 0 value length in Angularjs?

How to get question

open eyes
0 values length
in angularjs?

My Plunker


  • In my plunker I got the solution for (i) total no of questions (ii) total no of upvotes and (iii) Open eyes total sum of [value 1], what we expecting is (iv) how to get
    Open Eyes
    0 values
    length
    .

  • how to get
    Open Eyes
    [0 values]
    length
    , please look at my plunker for reference I don't know where I did the mistake

  • If I used this
    {{question.openeyes.length == '0'}
    the answer I got
    False
    ,
    Expecting answer would be :
    2



My 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>


My Data :-

$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"
}]



  • If any of one knows the solution please update
    my plunker
    as well to know the exact solution thanks


Answer Source

You can either create a filter or call a function to get the length of Open Eyes [0 values] length. But using a filter is more preferred because the latter has performance issue.

Method 1: Calling a function directly

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


Method 2 : Using Filter

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