简体   繁体   English

在 Angular2 中访问 Typescript 对象变量

[英]Access Typescript Object Variables in Angular2

How do I display the id and title of the below Hero object?如何显示以下 Hero 对象的 ID 和标题? The Hero interface below is mapped according to Firebase JSON response.下面的 Hero 接口是根据 Firebase JSON 响应映射的。

hero.component.ts hero.component.ts

import {Component, Input} from 'angular2/core';
import {Hero} from '../model/hero';

@Component({
  selector: 'hero-component',
  template: `
                {{hero | json }} this works. This display the Firebase JSON response as seen below 
                <br>
                {{ Object.keys(hero)[0] }} this DOESN'T work
                <br> 
                {{ hero[Object.keys(hero)[0]].title }}this also DOESN'T work
  `
})

export class HeroComponent {
  @Input()
  hero:Hero;
}

hero.ts英雄.ts

export interface Hero {
  [id: string]: {
    createdAt: number;
    isActive: boolean;
    title: string;
    updatedAt: number;
  }
}

Firebase JSON response Firebase JSON 响应

{ "-KEMOfA5KFK98AXNhPY0": { "createdAt": 1459607745222, "isActive": true, "title": "Wind Hero", "updatedAt": 1459607745222 } } 

update更新

instead of pipes: [KeysPipe]而不是pipes: [KeysPipe]

use

@NgModule({
  declarations: [KeysPipe],
  exports: [KeysPipe],
}
export class SharedModule{}
@NgModule({
  ...
  imports: [SharedModule],
})

original原来的

You can't use Object.keys(hero)[0] in the template.您不能在模板中使用Object.keys(hero)[0] No global references can be used, only members of the component.不能使用全局引用,只能使用组件的成员。

Instead create a function on your component而是在您的组件上创建一个函数

getKeys(obj) {
  return Object.keys(obj);
}

and then in the template然后在模板中

{{ getKeys(hero)[0] }}

Alternatively you can build a pipe that extracts the keys like for example https://stackoverflow.com/a/34074484/217408或者,您可以构建一个提取密钥的管道,例如https://stackoverflow.com/a/34074484/217408

@Pipe({ name: 'keys',  pure: false })
export class KeysPipe implements PipeTransform {
  transform(value: any, args: any[] = null): any {
    return Object.keys(value);
  }
}

then use it like然后像这样使用它

{{ (hero | keys)[0] }}

Don't forget to add the pipe to pipes: [KeysPipe] on the component where you want to use it or alternatively不要忘记将管道添加到pipes: [KeysPipe]在要使用它的组件上,或者

bootstrap(App, [provide(PLATFORM_PIPES, {useValue: KeysPipe, multi:true})]);

Your code isn't working since Object isn't available in your component.您的代码不起作用,因为Object在您的组件中不可用。

You could try something like this:你可以尝试这样的事情:

@Component({
  selector: 'hero-component',
  template: `
            {{hero | json }} this works. This display the Firebase JSON response as seen below 
            <br>
            {{ obj.keys(hero)[0] }} this DOESN'T work
            <br> 
            {{ hero[obj.keys(hero)[0]].title }}this also DOESN'T work
  `
})
export class HeroComponent {
  @Input()
  hero:Hero;

  obj = Object;
}

or using a method like Günter suggested in his answer.或者使用 Günter 在他的回答中建议的方法。

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

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