简体   繁体   English

访问angular2中没有管道或ngfor的对象

[英]Access objects without pipe or ngfor in angular2

Lets say that i have the following data asymmetrical array 可以说我有以下数据非对称数组

 [{'name':'user','password':'123'},{'title':'officer','grade':'5','age':'5'}]

from an ajax request that maps it 从一个映射它的ajax请求

return this._http.get(url).map( res => res.json() );

and i want to access it in angular2 html template, i directly know what each block corresponds to and it's technically not an array that you'd loop. 我想在angular2 html模板中访问它,我直接知道每个块对应什么,并且从技术上讲,这不是您要循环的数组。

Is there a method in angular to access the data without using a pipe or ngFor? 有没有一种方法可以不使用管道或ngFor来访问数据?

currently i'm using a really dirty method by slicing ngFor then using the dictionary data 目前我正在通过切片ngFor然后使用字典数据来使用一种非常脏的方法

<div *ngFor="let  i of element| slice:0:1">
<p> {{element.name}} </p>
<p> {{element.pass}} </p>
</div>
<div *ngFor="let  i of element| slice:1:2">
<p> {{element.title}}</p>
...

is there a simpler way with less setup to do this, because i keep encountering hundres of exceptions and i can't find a simple documented way to access JSON objects without the overhead setup? 有没有一种更简单的方法,用更少的设置就可以做到这一点,因为我一直遇到成百上千的异常,而且如果没有开销设置,就找不到一种简单的文档化方式来访问JSON对象?

You need to guard null values if you get the data async 如果数据异步,则需要保护空值

{{element && element[0].name}}
{{element && element[0].pass}}
{{element && element[1].title}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM