angular - 循环到 Typescript 中的对象

标签 angular typescript

顺便说一句,我是 Typescript 和 Angular2 的新手。

我有一个返回 PracticeTestList 类型的对象的服务。服务和对象的声明如下所示。

现在,我有一个读取对象的自定义管道,如下所示。

自定义管道类确实收到了该对象,但在for循环中,该对象被读取为单行字符串而不是对象。这是为什么?

如何在 Typescript 中将对象作为对象读取?

谢谢

服务

getMyPracticeTest(uid: string){
   return this._http.get('http://localhost:49753/RestServiceImpl.svc/getMyPracticeTest/' + uid)
    .map(data => {
        data.json();
        // the console.log(...) line prevents your code from working 
        // either remove it or add the line below (return ...)
        console.log("getMyPracticeTest >>>>>>> ", <PracticeTestList[]>data.json());
        return <PracticeTestList[]>data.json();
    });
}

对象声明

import { Injectable } from '@angular/core';

@Injectable()
export interface PracticeTestList {
    Purchase_ID: number;
    User_FK: number;
    name: string;
    price: number;
    resteurant: string;
    credit_card_number: string;
    purchase_date : any;
    Test_Status_FK: number;
    child :string;
}

自定义管道

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

@Pipe({name: 'values'})
export class ValuesPipe implements PipeTransform {
    transform(value, args:string[]) : any {
        let keys = [];

        for (let key in value) {
            console.log("Key >>>> " + key + "   value >>>>> " + value[key]);
            keys.push({key: key, value: value[key]});
        }

        return keys;
    }
}

在管道内添加日志

Key >>>> 0   value >>>>> [  main.bundle.js:64502:13
Key >>>> 1   value >>>>> {  main.bundle.js:64502:13
Key >>>> 2   value >>>>> "  main.bundle.js:64502:13
Key >>>> 3   value >>>>> P  main.bundle.js:64502:13
Key >>>> 4   value >>>>> u  main.bundle.js:64502:13
Key >>>> 5   value >>>>> r  main.bundle.js:64502:13
Key >>>> 6   value >>>>> c  main.bundle.js:64502:13
Key >>>> 7   value >>>>> h  main.bundle.js:64502:13
Key >>>> 8   value >>>>> a  main.bundle.js:64502:13
Key >>>> 9   value >>>>> s  main.bundle.js:64502:13
Key >>>> 10   value >>>>> e  main.bundle.js:64502:13
Key >>>> 11   value >>>>> _  main.bundle.js:64502:13
Key >>>> 12   value >>>>> I  main.bundle.js:64502:13
Key >>>> 13   value >>>>> D  main.bundle.js:64502:13
Key >>>> 14   value >>>>> "  main.bundle.js:64502:13
Key >>>> 15   value >>>>> :  main.bundle.js:64502:13
Key >>>> 16   value >>>>> 1  main.bundle.js:64502:13
Key >>>> 17   value >>>>> ,  main.bundle.js:64502:13
Key >>>> 18   value >>>>> "

添加了 HTML 代码

<table class="table" *ngIf="myPurchaseItems">
    <tr *ngFor="let entry  of myPurchaseItems | values">
      <td>Key: {{entry.key}}, value: {{entry.value}}</td>
    </tr>
</table>

最佳答案

首先使用Object.keys获取 key :

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'values' })
export class ValuesPipe implements PipeTransform {
  transform(value): any {
    let keys = Object.keys(value);
    return keys.map(k => value[k]);
  }
}

您可以尝试Object.values()直接获取值,但可能尚未在所有地方都支持。

关于angular - 循环到 Typescript 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841454/

相关文章:

javascript - 如何在 Typescript 2.1+ 中使用 Bluebird

json - Angular 2 - 在 json 对象中迭代 json 数组

jquery - 如何将 bootstrap 3 添加到基于 Angular2 webpack 的项目中

angular - 如何根据垫单选按钮的值禁用垫输入字段?

typescript - 如何检查一个对象是否只包含 Typescript 中指定的键?

angular - 已编译的 TypeScript 中导入的组件缺少 Require 语句

angular - 从服务获取新值不会重新加载 View

javascript - 为长度为 '[]' 的元组类型 '0' 构建 Angular 项目时出错,索引 '0' 处没有元素

angular - 展开运算符执行次数超过预期

node.js - 手动注册 Node 模块