我如何读取离子2和角2中的对象? [英] How i can read an Object within ionic 2 and angular 2?

查看:33
本文介绍了我如何读取离子2和角2中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从服务器获得了一个对象,我想在 ionic2 <$ c $中阅读 c>打字稿。

I've got an object from server and i want to read this within ionic2 typescript.

我的问题:

我如何为每个获得其值的键读取此对象吗?

示例:

Example:

ionic 带有 header 的列表:
在这种情况下 Header = key1,key2,key3 每个密钥

ionic list with header : in this case Header = key1 , key2 , key3 and the value is the values of every key

Header = key1
items1
items1
items1
Header2 = key2
items2
items2
items2..

对象:

{
    "status": "success",
    "products": {
        "Key1": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key2": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key3": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
    }
}


推荐答案

打印 * ngFor 循环中的数组,首先写入一个 Pipes

To print "Key" array in *ngFor loop, first write one Pipes

Pipe.ts

import { Component,Pipe, PipeTransform, Injectable } from '@angular/core';

@Pipe({
  name: 'objectValues'
})

@Injectable()
export class ObjectValuesPipe implements PipeTransform {
  transform(obj: any) {
    let result = [];
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        result.push(obj[key]);
      }
    }
    return result;
  }
}

不要忘记导入您的 @NgModule 中的管道,则可以像这样使用此管道。

do not forget to import your Pipes in @NgModule, than you can use this pipe like this.

<ul *ngFor="let item of items">
   <li *ngFor="let value of item | objectValues">
     {{ value }}
   </li>
 </ul>

基于:

Based On : How to display json object using *ngFor and access key,value of object

这篇关于我如何读取离子2和角2中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆