2017-08-22 9 views
1

質問を得るにはopen eyes0 values lengthはangularjsで?私は、(i)のためのソリューションを持って私のplunkerで Angularjsで0の値の長さを取得するには?

  • My Plunker

    は質問のない合計(ⅱ)upvotesのないとの(III)を開き目総和を合計しない[値1]、私たち(iv)Open Eyes 0値の取得方法は、lengthです。

  • Open Eyes [0の値が] length、私は間違い

  • をしたところ、私はこの{{question.openeyes.length == '0'}を使用した場合、私は知らない参照用my plunkerを見てください取得する方法私は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": "[email protected]", 
      "categories": [] 
     }, 
     "__v": 1, 
     "openeyers": ["[email protected]"], 
     "openeyes": 1, 
     "upvoters": ["[email protected]"], 
     "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": "[email protected]", 
      "categories": [] 
     }, 
     "__v": 1, 
     "openeyers": [], 
     "openeyes": 0, 
     "upvoters": ["[email protected]"], 
     "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": "[email protected]", 
      "categories": [] 
     }, 
     "__v": 1, 
     "openeyers": [], 
     "openeyes": 0, 
     "upvoters": [ "[email protected]", "[email protected]"], 
     "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": "[email protected]", 
      "categories": [] 
     }, 
     "__v": 1, 
     "openeyers": ["[email protected]"], 
     "openeyes": 1, 
     "upvoters": ["[email protected]","[email protected]", "[email protected]"], 
     "upvotes": 0, 
     "title": "he is best", 
     "created": "2017-06-06T12:58:40.204Z" 
}] 
  • いずれかが解決策を知っている場合は厳密解を知っているだけでなくmy plunkerを更新してください感謝
+0

あなたはグローバルとローカル変数に詰まっているようです。 –

+0

質問は配列です。あなたはどのインデックスを参照しようとしていますか? – Vivz

+0

@Vivzあなたのご意見ありがとうございます、私はあなたの答えを試してみましたが、可能ならば、私のプランカの感謝を更新します –

答えて

1

あなたは、フィルタを作成することができますまたは関数を呼び出して長さをOpen Eyes [0 values] lengthにします。しかし、フィルタを使用する方がパフォーマンス上の問題があるため、フィルタを使用する方がより好ましい方法です。

方法1JS

直接関数を呼び出す:

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

ワーキングP lunkerhttp://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 Plunkerhttp://plnkr.co/edit/RjNWeqWEYuzbBdew8s7V?p=preview

+0

あなたの貴重な答えをお寄せいただきありがとうございます。 –

+0

この '{{question.length}} 'の代わりに質問を数える可能性はありますか?それは私のプロジェクトでは機能していないからです。あなたが解決策を知っていれば更新してください.... –

+0

なぜあなたは答えを削除しましたか? – Vivz

関連する問題