2016-06-17 8 views
0

角度jsでng-repeatを使用してjsonファイルから値を表示しようとしていますが、それを行うことができません。ng-repeatを使って角度jのキー値にアクセスする方法は?

これは私がしようとしている私のコードです:

<div ng-repeat = "x in myWelcome.definition"> 
     {{x.attributes[0].children[0].node_id}} 
     <hr> 
    </div> 

を私はこれを試してみました、それが働いている:

 <!-- first attributes start --> 
    <div ng-repeat = "x in myWelcome.definition.attributes"> 
       {{x.rm_attribute_name}}<hr> 
      <div ng-repeat="a in x.children"> 

       <div ng-if ="a.attributes"> 
       a: {{a.attributes[0].rm_attribute_name}} 
        <div ng-if= "a.attributes[0].children"> 
        chld 
        </div> 
       </div> 

      </div> 

     </div> 
<!-- second attributes end --> 

私はこの行は{{働いているかを理解しようとしています。属性{0} .rm_attribute_name}}これは{{a.attributes 1 .rm_attribute_name}}のように動作しませんなぜこれは混乱します。 0とインデックスを使用しているときに、どのようにすべての結果を表示していますか? そして、私のJSONファイルはplunkerにここにある: Plunker link of json and code

それでは、どのように私はここにこのコードをngのリピートを使用して、ここで繰り返すことができます:

{{myWelcome.definition.attributes[0]}} 

はNGを使用して、私は私の見解では、これを表示することができますどのように取り組んでいます-repeat ng-repeatを使ってすべての属性と子を表示する必要があります。

<div ng-repeat = "(key,value) in myWelcome.definition.attributes"> 
     Key:{{key}} and value :{{value}} 
    <hr> 
    </div> 

ORあなたはこれを試すことができます:

+0

[好きですか](http://stackoverflow.com/questions/15127834/how-can-i-iterate-over-the-keys-value-in-ng-repeat-in-angular)を意味しますか? '(key。x)in myWelcome.definition' –

+0

キーと値知っているのは、ng-repeatだけを使って行うことができます。定義は私が子を持っているなどです。 –

+0

@Codingisgreat、http:// jsfiddle.net/Lvc0u55v/5529/ –

答えて

0

私はあなたがng-実装しようとしている見ることができますあなたのjsonファイルのifとng-repeat。 ng-repeatがどのように動作するのか理解する必要があります。{{$ index}}を使って配列のインデックスをいつでも確認できます。あなたの問題の種類のために、私はこれがあなたが試みるべき解決策だと思う。

<!-- first attributes start --> 
    <div ng-repeat = "x in myWelcome.definition.attributes"> 
       {{x.rm_attribute_name}}<hr> 
      <div ng-repeat="a in x.children"> 
        {{$index}} 
       <div ng-if ="a.attributes"> 
       <div ng-repeat="b in a.attributes"> 
         <hr> 
         {{b.children.length}} 
         <div ng-if="b.children.length > 1"> 
         If the array is more than 1 please do the magic here 
         </div> 
       </div> 
        <div ng-if= "a.attributes[0].children"> 
        chld 
        </div> 
       </div> 

      </div> 

     </div> 
<!-- second attributes end --> 

インデックスは{{$ index}}を使用していつでも見ることができます。値が1より大きいかどうかを調べるには常に.lengthを使用する必要があります。あなたはあなたが望むものを達成することができ、javascriptの配列について何かを学ぶ必要があり、点と括弧の間の違いは何ですか? http://www.dev-archive.net/articles/js-dot-notation/をお読みください。私はあなたがjavascriptの基礎を学ぶべきだと思います。

2

ngのリピートは以下のように用いることができる

<div ng-repeat = "x in myWelcome.definition.attributes"> 

    <div ng-repeat="a in x.children"> 
     a:{{a.node_id}} 
    </div>  
</div> 

Edited Plunker

+0

どうすれば値のように反復することができますか:

+0

@コーディングは私の答えの中でplunkerを確認してください。それはあなたを助けるでしょう:) – iAshay

+0

[this](https://docs.angularjs.org/api/ng/directive/ngRepeat)ドキュメントも参照できます –

関連する問題