如何访问数组索引以进行有角度的有序打印?

时间:2018-08-29 14:13:39

标签: arrays json angular key

在json数组中,类似:

[{title: 'title1',
details: [{text: 'text1'},
    {code: 'code1'},
    {text: 'text2'},
    {code: 'code2'}, 
    {array: array1}]
...
},
{{title: 'title2',
details: [{text: 'text1'},
    {text: 'text2'},
    {array: array1},
    {code: 'code1'}, 
...

我想打印3种不同类型的数据:文本,数组和代码。 文字任何问题,用

数组
<table> 

并使用highlight module

进行编码
<pre><code highlight [code]="..."></code></pre>

然后,我的第一张纸应该是:

text1
code1 in code style
text2
code2 in code style
array1 in array style

第二个:

text1
text2
array1 in array style
code1  in code style

我有一些使用* ngfor循环和ngif进行此操作的想法,但是如何访问键文本,表和代码来执行ngif? 还是看到更简单的解决方案?...

1 个答案:

答案 0 :(得分:0)

首先,您在第一个元素上具有无效的数组,它具有重复的键code。您应该使用1键作为code,然后将所有代码的对象放在该索引上。但是,如果这只是一个示例,并且您具有正确的数组和对象语法,则可以使用*ngFor对数组进行迭代。

<div *ngFor="let elem of yourArray;" >

<p class="text">{{elem.text}}</p>

<pre><code highlight [code]={{elem.code}}></code></pre>

<!-- And it goes with all the elements you want to display. -->

</div>