2017-01-25 12 views
1

私はobjを持っていますが、これはdbクエリから戻ってきます。私は "連絡先"配列から単一のレコードを表示できるようにしたいが、これを行うための最良の方法は今だと確信している。ここでオブジェクト配列から単一のレコードを返します

は一例として、私のクエリからの出力である:私の角度のコードで

{ 
    "_id": "5886e3692ca4542c453431ee", 
    "initial": "u", 
    "last_name": "Peterson", 
    "first_name": "Pete", 
    "owner_id": "5886e32c2ca4542c453431ed", 
    "__v": 1, 
    "contacts": [ 
     { 
      "last_name": "Eltoro", 
      "first_name": "Peter", 
      "_id": "5886e3f5a219472cac886a9f", 
      "businesses": [], 
      "addresses": [], 
      "phones": [ 
       { 
        "phone_type": "mobile", 
        "phone_number": "555-555-999", 
        "_id": "5886e3f5a219472cac886aa2" 
       }, 
       { 
        "phone_type": "home", 
        "phone_number": "999-876-000", 
        "_id": "5886e3f5a219472cac886aa1" 
       } 
       ], 
       "emails": [ 
       { 
        "email_address": "[email protected]", 
        "_id": "5886e3f5a219472cac886aa0" 
       } 
      ] 
     }, 
     { 
      "college": "University of WA", 
      "highschool": "Kalaheo", 
      "birthday": "1990-12-22T08:00:00.000Z", 
      "last_name": "Smith", 
      "first_name": "Suzanne", 
      "_id": "5886fb7fac288c2e31eabe0a", 
      "businesses": [], 
      "addresses": [ 
       { 
        "zip": "98777", 
        "state": "WA", 
        "city": "Seattle", 
        "address_2": "Apt 234", 
        "address": "124 194th St. SW", 
        "_id": "5886fb7fac288c2e31eabe0e" 
       } 
      ], 
      "phones": [ 
       { 
        "phone_type": "mobile", 
        "phone_number": "206-899-9898", 
        "_id": "5886fb7fac288c2e31eabe0d" 
       }, 
       { 
        "phone_type": "home", 
        "phone_number": "206-789-0987", 
        "_id": "5886fb7fac288c2e31eabe0c" 
       } 
      ], 
      "emails": [ 
       { 
        "email_type": "personal", 
        "email_address": "[email protected]", 
        "_id": "5886fb7fac288c2e31eabe0b" 
       } 
      ] 
     }, 
     { 
      "last_name": "Alabaster", 
      "first_name": "Cindy", 
      "_id": "5888b0bd3c025e54476828d9", 
      "businesses": [], 
      "addresses": [], 
      "phones": [ 
       { 
        "phone_type": "home", 
        "phone_number": "999-999-0909", 
        "_id": "5888b0bd3c025e54476828dc" 
       }, 
       { 
        "phone_type": "home", 
        "phone_number": "000-000-0000", 
        "_id": "5888b0bd3c025e54476828db" 
       } 
      ], 
      "emails": [ 
       { 
        "email_address": "[email protected]", 
        "_id": "5888b0bd3c025e54476828da" 
       } 
      ] 
     } 
    ], 
    "businesses": [], 
    "addresses": [], 
    "phones": [ 
     { 
      "phone_number": "999-999-9999", 
      "phone_type": "mobile", 
      "_id": "5886e37f2ca4542c453431ef" 
     } 
    ], 
    "emails": [] 
} 

、私は「連絡先」レコードの1つを取得し、それを表示したいです。

これを行うには、ng-repeatを実行し、idでフィルタリングするのが最善の方法ですか?

+0

ちょっと分かりますか?常に同じ連絡先を選択して表示しますか?または、フィルタリングできるリストを表示しますか? – Dario

+0

連絡先の情報を表示するだけです。 – cnak2

+0

この場合、連絡先をあなたのコントローラのスコープ変数$ scope.contact = data.contacts [0]; ''に割り当て、この変数をHTMLテンプレートにバインドするだけです: '' ' { {contact.first_name}} '' ' – Dario

答えて

0

私はあなたが一つだけ接触(例えば最初の)のための情報を表示したい場合は、次のように行うことができますngのリピートを使用して

$scope.selectedId = "0" 

$scope.yourJson = {...} 

<input type="text" ng-model="selectedId"></input> // you can enter an id of a contact 

<div ng-repeat="contact in yourJson.contacts | filter:{_id: selectedId}"> 
    {{contact.last_name}} 
</div> // it will show the last_name of the contact of which you inputed the id 
+0

ありがとうございましたArno、それは私がやっている計画ですが、より良い方法があるかどうか疑問に思っています。 – cnak2

+0

@ cnak2それはperformantです。本質的に、これは何が起こっているかを返すArray.prototype.filter.call(array、predicateFn); (角度ソースコードから) –

0

をフィルタリングします:

<p>{{yourObjectName.contacts[0].last_name}}</p> 
<p>{{yourObjectName.contacts[0].first_name}}</p> 
... 
<ul> 
    <li ng-repeat="phone in yourObjectName.contacts[0].phones"> 
     Phone type: {{phone.phone_type}}, phone number: {{phone.phone_number}} 
    </li> 
</ul> 
関連する問題