简体   繁体   English

Angular2如何仅从ngFor中获取一个值

[英]Angular2 How to get only one value from ngFor

I am currently getting deep nested json objects from the server and using pipe to parse them. 我目前正在从服务器获取深度嵌套的json对象,并使用管道对其进行解析。

But it is looping every key even tho I want to get only one value. 但是,即使我只想获得一个值,它也会循环遍历每个键。 How could I achieve this? 我怎样才能做到这一点?

These are codes 这些是代码

Pipe

import { Pipe, PipeTransform } from '@angular/core'; 从“ @ angular / core”导入{Pipe,PipeTransform};

@Pipe({name: 'keyValues'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push({key: key, value: value[key]});
    }
    return keys;
   }
 }

Html template HTML模板

 <div *ngFor="let detail of teaInfo | keyValues">
     <div *ngFor="let basicinfo of detail.value | keyValues">
            <p>School: {{basicinfo.value}}</p>
     </div>
 </div>

Result 结果

school:example1

school:example2

school:example3

school:example4

school:example5

school:example6

I also tried this 我也尝试过

  <div *ngFor="let detail of teaInfo | keyValues">
         <div *ngFor="let basicinfo of detail.value | keyValues">
                 <p>School: {{basicinfo.value['example2']}}</p>
          </div>
      </div>

result 结果

 school:

 school:example2

 school:

 school:

 school:

 school:

still looping without giving value.. 仍然循环而没有给予任何价值。

But I just want to get 但我只想得到

school:example2

without looping anything.. 没有任何循环..

Help will be appreciated! 帮助将不胜感激!

这可能会满足您的要求:

<div *ngFor="let basicinfo of detail.value | keyValues | splice:2:1">

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

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